From c87c5b86c284ad59750fe893eb7a17e0e76a8ec8 Mon Sep 17 00:00:00 2001 From: Rahix Date: Mon, 19 Aug 2024 03:32:51 +0200 Subject: [PATCH] Display rectangle --- src/main.py | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/main.py b/src/main.py index 89ba508..5979426 100644 --- a/src/main.py +++ b/src/main.py @@ -3,7 +3,7 @@ import pymupdf import tkinter from tkinter import ttk -from PIL import Image, ImageTk +from PIL import Image, ImageTk, ImageDraw CURRENT_PAGE = 0 RECT_START = None @@ -45,8 +45,15 @@ def main() -> None: root = tkinter.Tk() root.title("PDF Navigation Annotator Tool") + + root.columnconfigure(0, weight=10) + root.rowconfigure(0, weight=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") toc_tree = ttk.Treeview(frm, columns=columns, show="headings") @@ -63,6 +70,23 @@ def main() -> None: page_view = ttk.Label(frm) 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): global RECT_START if event.num != 1: @@ -76,23 +100,13 @@ def main() -> None: return if RECT_START is not None: 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) RECT_START = None page_view.bind("", start_rect) page_view.bind("", 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(): global CURRENT_PAGE