Display rectangle

This commit is contained in:
Rahix 2024-08-19 03:32:51 +02:00
parent 635800b923
commit c87c5b86c2

View file

@ -3,7 +3,7 @@ import pymupdf
import tkinter import tkinter
from tkinter import ttk from tkinter import ttk
from PIL import Image, ImageTk from PIL import Image, ImageTk, ImageDraw
CURRENT_PAGE = 0 CURRENT_PAGE = 0
RECT_START = None RECT_START = None
@ -45,8 +45,15 @@ def main() -> None:
root = tkinter.Tk() root = tkinter.Tk()
root.title("PDF Navigation Annotator Tool") root.title("PDF Navigation Annotator Tool")
root.columnconfigure(0, weight=10)
root.rowconfigure(0, weight=10)
frm = ttk.Frame(root, padding=10) frm = ttk.Frame(root, padding=10)
frm.grid() frm.grid(sticky="NSEW")
frm.columnconfigure(2, weight=10)
frm.rowconfigure(1, weight=10)
columns = ("title", "page") columns = ("title", "page")
toc_tree = ttk.Treeview(frm, columns=columns, show="headings") toc_tree = ttk.Treeview(frm, columns=columns, show="headings")
@ -63,6 +70,23 @@ def main() -> None:
page_view = ttk.Label(frm) page_view = ttk.Label(frm)
page_view.grid(column=2, row=1) page_view.grid(column=2, row=1)
def load_page(page_num: int, rect_overlay=None):
page = doc[page_num]
pix = page.get_pixmap(dpi=120)
mode = "RGBA" if pix.alpha else "RGB"
img = Image.frombytes(mode, [pix.width, pix.height], pix.samples)
if rect_overlay is not None:
draw = ImageDraw.Draw(img)
draw.rectangle(rect_overlay, outline="red", width=2)
img = ImageTk.PhotoImage(img)
page_view.configure(image=img)
page_view.image = img
page_title.configure(text=toc_entry_for_page(page_num))
def start_rect(event): def start_rect(event):
global RECT_START global RECT_START
if event.num != 1: if event.num != 1:
@ -76,23 +100,13 @@ def main() -> None:
return return
if RECT_START is not None: if RECT_START is not None:
print(f"UP @ {event.x}/{event.y}") print(f"UP @ {event.x}/{event.y}")
load_page(CURRENT_PAGE, [*RECT_START, event.x, event.y])
AddLinkDialog(root, [*RECT_START, event.x, event.y], toc) AddLinkDialog(root, [*RECT_START, event.x, event.y], toc)
RECT_START = None RECT_START = None
page_view.bind("<ButtonPress>", start_rect) page_view.bind("<ButtonPress>", start_rect)
page_view.bind("<ButtonRelease>", end_rect) page_view.bind("<ButtonRelease>", end_rect)
def load_page(page_num: int):
page = doc[page_num]
pix = page.get_pixmap()
mode = "RGBA" if pix.alpha else "RGB"
img = Image.frombytes(mode, [pix.width, pix.height], pix.samples)
img = ImageTk.PhotoImage(img)
page_view.configure(image=img)
page_view.image = img
page_title.configure(text=toc_entry_for_page(page_num))
def next_page(): def next_page():
global CURRENT_PAGE global CURRENT_PAGE