From 6908981a311d3d0363550f29b27e37097de4c6a2 Mon Sep 17 00:00:00 2001 From: jerome-labidurie Date: Wed, 4 Oct 2023 22:00:14 +0200 Subject: [PATCH 1/4] Resize the image to 512xSomething keeping the aspect ratio of the original --- image tag editor.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/image tag editor.py b/image tag editor.py index 6c31cb7..f4b5082 100644 --- a/image tag editor.py +++ b/image tag editor.py @@ -33,7 +33,7 @@ canvas = tk.Tk() canvas.grid() -canvas.geometry("1448x768") +# canvas.geometry("1448x768") # no predefined size, will take what's inside size canvas.title("tag editor") canvas.config(bg='white') @@ -58,7 +58,7 @@ def update_text_file(text, file_path): -# Function to update the image displayed in the +# Function to update the image displayed in the def update_image(): global current_image if current_image: @@ -74,8 +74,13 @@ def update_image(): create_text_file("", current_text_file) print("created " + image_names[current_image_index] + ".txt") + # resize image, but keep ratio + width = 512 + ratio = width / float(img.size[0]) + height = int (float(img.size[1]) * ratio) + print ("resize image for display to %dx%d with ratio %.2f" % (width, height, ratio) ) - img = img.resize((512, 768), Image.LANCZOS) + img = img.resize((width, height), Image.LANCZOS) img = ImageTk.PhotoImage(img) @@ -106,13 +111,13 @@ def on_key_press(event): def load_text_file(file): - with open(file, "r") as f: + with open(file, "r") as f: content = f.read() entry.insert(INSERT, content) def save_text_file(file): text = str(entry.get(1.0, END)) - with open(file, "w") as f: + with open(file, "w") as f: f.write(text) def clearFile(): @@ -166,4 +171,4 @@ def saveAndExitButton(): entry.grid(row=0,column=1,sticky="nsew", padx=15, pady=(15, 60)) update_image() -canvas.mainloop() \ No newline at end of file +canvas.mainloop() From f5bb4bfb9601e361485321778de6968b56eac9ca Mon Sep 17 00:00:00 2001 From: jerome-labidurie Date: Thu, 5 Oct 2023 19:10:37 +0200 Subject: [PATCH 2/4] Make content fill and resize with window --- README.md | 5 +++-- image tag editor.py | 49 +++++++++++++++++++++++---------------------- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 857b10c..df0581c 100644 --- a/README.md +++ b/README.md @@ -13,5 +13,6 @@ it will automatically save over the current text file when you hit page up or pa It's probably buggy so might wanna back up your files first in another directory. -I've just hardcoded the display resolution to 512x768, cause that's what I was working with and I couldn't be bothered figuring out how to make it dynamic based on the images. If you can tell me how please do and I'll update it. -Only been tested in windows 10, with python 3.11 +Only been tested in +* windows 10, with python 3.11 +* ubuntu 22.04 with python 3.10 diff --git a/image tag editor.py b/image tag editor.py index f4b5082..fab03d9 100644 --- a/image tag editor.py +++ b/image tag editor.py @@ -30,22 +30,27 @@ # Create a variable to keep track of the current image index current_image_index = 0 - canvas = tk.Tk() canvas.grid() # canvas.geometry("1448x768") # no predefined size, will take what's inside size canvas.title("tag editor") canvas.config(bg='white') -frame = tk.Frame(canvas) -frame.grid(row=0, column=0, sticky="nsew") +# Create frame to hold the text editor and buttons +text_frame = Frame(canvas, bg = "#1e1319") +text_frame.grid(row=0, column=1, sticky="nsew") + +# label for image display +label = Label(canvas) +label.grid(row=0,column=0,sticky="nsew") +# make re-sizable +canvas.rowconfigure(0, weight=1) +canvas.columnconfigure(0, weight=0) +canvas.columnconfigure(1, weight=1) -# Create a bottom frame to hold the text editor and image -text_frame = Frame(frame) -text_frame.grid(row=1, column=1, sticky="ew") -text_frame.grid_rowconfigure(0, weight=0) -text_frame.grid_columnconfigure(1, weight=0) +text_frame.rowconfigure(0, weight=1) +text_frame.columnconfigure(1, weight=1) def create_text_file(text, file_path): @@ -83,9 +88,7 @@ def update_image(): img = img.resize((width, height), Image.LANCZOS) img = ImageTk.PhotoImage(img) - - label = Label(text_frame, image=img) - label.grid(row=0,column=0,sticky="nsew") + label.configure(image=img) label.image = img # Keep a reference to the image @@ -146,14 +149,12 @@ def saveAndExitButton(): exit() - # Create a top frame to hold the buttons button_frame = Frame(text_frame, bg="#1e1319") -button_frame.grid(row=0, column=1, sticky="nsew") -button_frame.grid_rowconfigure(0, weight=1) -button_frame.grid_columnconfigure(1, weight=0) -button_frame.grid_columnconfigure(2, weight=0) -button_frame.grid_columnconfigure(3, weight=1) +button_frame.grid(row=1, column=1, sticky="se") + +# for c in range(0,5): +# button_frame.columnconfigure(c, weight=1) b5 = Button(button_frame, text=" Save and Exit ", bg = "#4f2d3f", command = saveAndExitButton) b4 = Button(button_frame, text=" Exit ", bg = "#4f2d3f", command = exit) @@ -161,14 +162,14 @@ def saveAndExitButton(): b2 = Button(button_frame, text=" Save ", bg = "#4f2d3f", command = saveFileButton) b1 = Button(button_frame, text=" Open ", bg = "#4f2d3f", command = openFileButton) -b5.grid(row=1, column=4, padx=20, pady=20, sticky="W") -b4.grid(row=1, column=3, padx=20, pady=20, sticky="W") -b3.grid(row=1, column=2, padx=20, pady=20, sticky="W") -b2.grid(row=1, column=1, padx=20, pady=20, sticky="W") -b1.grid(row=1, column=0, padx=(450, 20), pady=20, sticky="W") +b5.grid(row=1, column=4, padx=20, pady=20, sticky="E") +b4.grid(row=1, column=3, padx=20, pady=20, sticky="E") +b3.grid(row=1, column=2, padx=20, pady=20, sticky="E") +b2.grid(row=1, column=1, padx=20, pady=20, sticky="E") +b1.grid(row=1, column=0, padx=20, pady=20, sticky="E") -entry = Text(text_frame, wrap = WORD, bg = "#281b22", fg="#d8adc3", insertbackground="yellow", font = ("poppins", 15), padx=10, pady=10) -entry.grid(row=0,column=1,sticky="nsew", padx=15, pady=(15, 60)) +entry = Text(text_frame, wrap = WORD, bg = "#281b22", fg="#d8adc3", insertbackground="yellow", font = ("poppins", 15)) +entry.grid(row=0,column=1,sticky="nsew", padx=10, pady=10) update_image() canvas.mainloop() From 24d283943aa59d8a50af441cb9ffe4c5aa830ad5 Mon Sep 17 00:00:00 2001 From: jerome-labidurie Date: Thu, 5 Oct 2023 19:13:59 +0200 Subject: [PATCH 3/4] Align buttons with textbox --- image tag editor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/image tag editor.py b/image tag editor.py index fab03d9..6dbb50b 100644 --- a/image tag editor.py +++ b/image tag editor.py @@ -162,7 +162,7 @@ def saveAndExitButton(): b2 = Button(button_frame, text=" Save ", bg = "#4f2d3f", command = saveFileButton) b1 = Button(button_frame, text=" Open ", bg = "#4f2d3f", command = openFileButton) -b5.grid(row=1, column=4, padx=20, pady=20, sticky="E") +b5.grid(row=1, column=4, padx=10, pady=20, sticky="E") b4.grid(row=1, column=3, padx=20, pady=20, sticky="E") b3.grid(row=1, column=2, padx=20, pady=20, sticky="E") b2.grid(row=1, column=1, padx=20, pady=20, sticky="E") From 49d419c6c0b80c3d2c3cf5731b9305e4cb3a80d4 Mon Sep 17 00:00:00 2001 From: jerome-labidurie Date: Thu, 5 Oct 2023 19:22:05 +0200 Subject: [PATCH 4/4] Fix open/save file handling --- image tag editor.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/image tag editor.py b/image tag editor.py index 6dbb50b..86e8e23 100644 --- a/image tag editor.py +++ b/image tag editor.py @@ -127,7 +127,7 @@ def clearFile(): entry.delete(1.0, END) def saveFileButton(): - new_file = asksaveasfile(mode = 'w', filetype = [('text files', '.txt')]) + new_file = asksaveasfile(mode = 'w', filetypes = [('text files', '.txt')]) if new_file is None: return text = str(entry.get(1.0, END)) @@ -135,10 +135,10 @@ def saveFileButton(): new_file.close() def openFileButton(): - file = askopenfile(mode = 'r', filetype = [('text files', '*.txt')]) + file = askopenfile(mode = 'r', filetypes = [('text files', '*.txt')]) if file is not None: content = file.read() - entry.insert(INSERT, content) + entry.insert(INSERT, content) def clearFileButton(): entry.delete(1.0, END)