Skip to content

Instantly share code, notes, and snippets.

@2blackbar
Created August 14, 2023 13:38
Show Gist options
  • Save 2blackbar/1552c4e4bee20ae875f0838c5fe40ab4 to your computer and use it in GitHub Desktop.
Save 2blackbar/1552c4e4bee20ae875f0838c5fe40ab4 to your computer and use it in GitHub Desktop.
import os
import glob
import tkinter as tk
from PIL import Image, ImageTk
class ImageGallery:
def __init__(self, master, folder_path):
self.master = master
self.folder_path = folder_path
self.image_files = [f for f in os.listdir(folder_path) if f.endswith('.png') or f.endswith('.jpg') or f.endswith('.jpeg')]
self.current_image_index = 0
self.image_label = tk.Label(self.master)
self.image_label.pack()
self.new_text_frame = tk.Frame(self.master)
self.new_text_frame.pack()
self.new_text_label = tk.Label(self.new_text_frame, text='ADD TAG TO ALL TXT FILES:', font=('TkDefaultFont', 24), fg='red')
self.new_text_label.pack(side=tk.LEFT)
self.new_text_entry = tk.Entry(self.new_text_frame, font=('TkDefaultFont', 24))
self.new_text_entry.pack(side=tk.RIGHT)
# Change the color of the "ADD LEFT" button to red
self.add_left_button = tk.Button(self.new_text_frame, text='ADD LEFT', command=self.add_left, height=2, width=20, bg='red', font=('Helvetica', 12, 'bold'))
self.add_left_button.pack(side=tk.LEFT)
# Add a new "ADD RIGHT" button to the GUI
self.add_right_button = tk.Button(self.new_text_frame, text='ADD RIGHT', command=self.add_right, height=2, width=20, bg='red', font=('Helvetica', 12, 'bold'))
self.add_right_button.pack(side=tk.RIGHT)
# Clone the ADD TAG frame and add it below the current one
self.new_text_frame2 = tk.Frame(self.master)
self.new_text_frame2.pack()
self.new_text_label2 = tk.Label(self.new_text_frame2, text='ADD TAG:', font=('TkDefaultFont', 24))
self.new_text_label2.pack(side=tk.LEFT)
self.new_text_entry2 = tk.Entry(self.new_text_frame2, font=('TkDefaultFont', 24))
self.new_text_entry2.pack(side=tk.RIGHT)
# Add new "ADD LEFT" and "ADD RIGHT" buttons to the cloned frame
self.add_left_button2 = tk.Button(self.new_text_frame2, text='ADD LEFT', command=self.add_left2, height=2, width=20, bg='red', font=('Helvetica', 12, 'bold'))
self.add_left_button2.pack(side=tk.LEFT)
self.add_right_button2 = tk.Button(self.new_text_frame2, text='ADD RIGHT', command=self.add_right2, height=2, width=20, bg='red', font=('Helvetica', 12, 'bold'))
self.add_right_button2.pack(side=tk.RIGHT)
# Clone the ADD TAG frame again and add it below the previous one
self.new_text_frame3 = tk.Frame(self.master)
self.new_text_frame3.pack()
self.new_text_label3 = tk.Label(self.new_text_frame3, text='ADD TAG:', font=('TkDefaultFont', 24))
self.new_text_label3.pack(side=tk.LEFT)
self.new_text_entry3 = tk.Entry(self.new_text_frame3, font=('TkDefaultFont', 24))
self.new_text_entry3.pack(side=tk.RIGHT)
# Add new "ADD LEFT" and "ADD RIGHT" buttons to the second cloned frame
self.add_left_button3 = tk.Button(self.new_text_frame3, text='ADD LEFT', command=self.add_left3, height=2, width=20, bg='red', font=('Helvetica', 12, 'bold'))
self.add_left_button3.pack(side=tk.LEFT)
self.add_right_button3 = tk.Button(self.new_text_frame3, text='ADD RIGHT', command=self.add_right3, height=2, width=20, bg='red', font=('Helvetica', 12, 'bold'))
self.add_right_button3.pack(side=tk.RIGHT)
# Clone the ADD TAG frame again and add it below the previous one
self.new_text_frame4 = tk.Frame(self.master)
self.new_text_frame4.pack()
self.new_text_label4 = tk.Label(self.new_text_frame4, text='ADD TAG:', font=('TkDefaultFont', 24))
self.new_text_label4.pack(side=tk.LEFT)
self.new_text_entry4 = tk.Entry(self.new_text_frame4, font=('TkDefaultFont', 24))
self.new_text_entry4.pack(side=tk.RIGHT)
# Add new "ADD LEFT" and "ADD RIGHT" buttons to the second cloned frame
self.add_left_button4 = tk.Button(self.new_text_frame4, text='ADD LEFT', command=self.add_left4, height=2, width=20, bg='red', font=('Helvetica', 12, 'bold'))
self.add_left_button4.pack(side=tk.LEFT)
self.add_right_button4 = tk.Button(self.new_text_frame4, text='ADD RIGHT', command=self.add_right4, height=2, width=20, bg='red', font=('Helvetica', 12, 'bold'))
self.add_right_button4.pack(side=tk.RIGHT)
# Clone the ADD TAG frame again and add it below the previous one
self.new_text_frame5 = tk.Frame(self.master)
self.new_text_frame5.pack()
self.new_text_label5 = tk.Label(self.new_text_frame5, text='ADD TAG:', font=('TkDefaultFont', 24))
self.new_text_label5.pack(side=tk.LEFT)
self.new_text_entry5 = tk.Entry(self.new_text_frame5, font=('TkDefaultFont', 24))
self.new_text_entry5.pack(side=tk.RIGHT)
# Add new "ADD LEFT" and "ADD RIGHT" buttons to the second cloned frame
self.add_left_button5 = tk.Button(self.new_text_frame5, text='ADD LEFT', command=self.add_left5, height=2, width=20, bg='red', font=('Helvetica', 12, 'bold'))
self.add_left_button5.pack(side=tk.LEFT)
self.add_right_button5 = tk.Button(self.new_text_frame5, text='ADD RIGHT', command=self.add_right5, height=2, width=20, bg='red', font=('Helvetica', 12, 'bold'))
self.add_right_button5.pack(side=tk.RIGHT)
# Clone the ADD TAG frame again and add it below the previous one
self.new_text_frame6 = tk.Frame(self.master)
self.new_text_frame6.pack()
self.new_text_label6 = tk.Label(self.new_text_frame6, text='ADD TAG:', font=('TkDefaultFont', 24))
self.new_text_label6.pack(side=tk.LEFT)
self.new_text_entry6 = tk.Entry(self.new_text_frame6, font=('TkDefaultFont', 24))
self.new_text_entry6.pack(side=tk.RIGHT)
# Add new "ADD LEFT" and "ADD RIGHT" buttons to the second cloned frame
self.add_left_button6 = tk.Button(self.new_text_frame6, text='ADD LEFT', command=self.add_left6, height=2, width=20, bg='red', font=('Helvetica', 12, 'bold'))
self.add_left_button6.pack(side=tk.LEFT)
self.add_right_button6 = tk.Button(self.new_text_frame6, text='ADD RIGHT', command=self.add_right6, height=2, width=20, bg='red', font=('Helvetica', 12, 'bold'))
self.add_right_button6.pack(side=tk.RIGHT)
# Clone the ADD TAG frame again and add it below the previous one
self.new_text_frame7 = tk.Frame(self.master)
self.new_text_frame7.pack()
self.new_text_label7 = tk.Label(self.new_text_frame7, text='ADD TAG:', font=('TkDefaultFont', 24))
self.new_text_label7.pack(side=tk.LEFT)
self.new_text_entry7 = tk.Entry(self.new_text_frame7, font=('TkDefaultFont', 24))
self.new_text_entry7.pack(side=tk.RIGHT)
# Add new "ADD LEFT" and "ADD RIGHT" buttons to the second cloned frame
self.add_left_button7 = tk.Button(self.new_text_frame7, text='ADD LEFT', command=self.add_left7, height=2, width=20, bg='red', font=('Helvetica', 12, 'bold'))
self.add_left_button7.pack(side=tk.LEFT)
self.add_right_button7 = tk.Button(self.new_text_frame7, text='ADD RIGHT', command=self.add_right7, height=2, width=20, bg='red', font=('Helvetica', 12, 'bold'))
self.add_right_button7.pack(side=tk.RIGHT)
self.description_text = tk.Text(self.master, wrap=tk.WORD, font=('TkDefaultFont', 24), height=10)
self.description_text.pack()
self.back_button = tk.Button(self.master, text='BACK', command=self.show_previous_image, height=2, width=20, bg='green', font=('Helvetica', 12, 'bold'))
self.back_button.pack(side=tk.LEFT)
self.next_button = tk.Button(self.master, text='NEXT', command=self.show_next_image, height=2, width=20, bg='green', font=('Helvetica', 12, 'bold'))
self.next_button.pack(side=tk.RIGHT)
self.save_button = tk.Button(self.master, text='SAVE', command=self.save_description, height=2, width=20, bg='red', font=('Helvetica', 14, 'bold'))
self.save_button.pack(side=tk.BOTTOM)
self.show_current_image()
def add_left(self):
new_text = self.new_text_entry.get().strip()
if new_text:
for file in glob.glob("*.txt"):
with open(file, 'r') as f:
description = new_text + ' ' + f.read().strip()
with open(file, 'w') as f:
f.write(description)
self.description_text.delete('1.0', tk.END)
self.description_text.insert(tk.END, description)
def add_right(self):
new_text = self.new_text_entry.get().strip()
if new_text:
for file in glob.glob("*.txt"):
with open(file, 'r') as f:
description = f.read().strip() + ' ' + new_text
with open(file, 'w') as f:
f.write(description)
self.description_text.delete('1.0', tk.END)
self.description_text.insert(tk.END, description)
def add_left2(self):
new_text = self.new_text_entry2.get().strip()
if new_text:
description = new_text + ' ' + self.description_text.get('1.0', tk.END).strip()
self.description_text.delete('1.0', tk.END)
self.description_text.insert(tk.END, description)
def add_right2(self):
new_text = self.new_text_entry2.get().strip()
if new_text:
description = self.description_text.get('1.0', tk.END).strip() + ' ' + new_text
self.description_text.delete('1.0', tk.END)
self.description_text.insert(tk.END, description)
def add_left3(self):
new_text = self.new_text_entry3.get().strip()
if new_text:
description = new_text + ' ' + self.description_text.get('1.0', tk.END).strip()
self.description_text.delete('1.0', tk.END)
self.description_text.insert(tk.END, description)
def add_right3(self):
new_text = self.new_text_entry3.get().strip()
if new_text:
description = self.description_text.get('1.0', tk.END).strip() + ' ' + new_text
self.description_text.delete('1.0', tk.END)
self.description_text.insert(tk.END, description)
def add_left4(self):
new_text = self.new_text_entry4.get().strip()
if new_text:
description = new_text + ' ' + self.description_text.get('1.0', tk.END).strip()
self.description_text.delete('1.0', tk.END)
self.description_text.insert(tk.END, description)
def add_right4(self):
new_text = self.new_text_entry4.get().strip()
if new_text:
description = self.description_text.get('1.0', tk.END).strip() + ' ' + new_text
self.description_text.delete('1.0', tk.END)
self.description_text.insert(tk.END, description)
def add_left5(self):
new_text = self.new_text_entry5.get().strip()
if new_text:
description = new_text + ' ' + self.description_text.get('1.0', tk.END).strip()
self.description_text.delete('1.0', tk.END)
self.description_text.insert(tk.END, description)
def add_right5(self):
new_text = self.new_text_entry5.get().strip()
if new_text:
description = self.description_text.get('1.0', tk.END).strip() + ' ' + new_text
self.description_text.delete('1.0', tk.END)
self.description_text.insert(tk.END, description)
def add_left6(self):
new_text = self.new_text_entry6.get().strip()
if new_text:
description = new_text + ' ' + self.description_text.get('1.0', tk.END).strip()
self.description_text.delete('1.0', tk.END)
self.description_text.insert(tk.END, description)
def add_right6(self):
new_text = self.new_text_entry6.get().strip()
if new_text:
description = self.description_text.get('1.0', tk.END).strip() + ' ' + new_text
self.description_text.delete('1.0', tk.END)
self.description_text.insert(tk.END, description)
def add_left7(self):
new_text = self.new_text_entry7.get().strip()
if new_text:
description = new_text + ' ' + self.description_text.get('1.0', tk.END).strip()
self.description_text.delete('1.0', tk.END)
self.description_text.insert(tk.END, description)
def add_right7(self):
new_text = self.new_text_entry7.get().strip()
if new_text:
description = self.description_text.get('1.0', tk.END).strip() + ' ' + new_text
self.description_text.delete('1.0', tk.END)
self.description_text.insert(tk.END, description)
def show_current_image(self):
image_file = self.image_files[self.current_image_index]
image_path = os.path.join(self.folder_path, image_file)
image = Image.open(image_path)
photo = ImageTk.PhotoImage(image)
self.image_label.config(image=photo)
self.image_label.image = photo
description_file = os.path.splitext(image_file)[0] + '.txt'
description_path = os.path.join(self.folder_path, description_file)
if os.path.exists(description_path):
with open(description_path, 'r') as f:
description = f.read()
# Remove the code that adds the ADD TAG to the description
# new_text = self.new_text_entry.get().strip()
# if new_text:
# description = new_text + ' ' + description
self.description_text.delete('1.0', tk.END)
self.description_text.insert(tk.END, description)
else:
# Remove the code that adds the ADD TAG to the description
# new_text = self.new_text_entry.get().strip()
# if new_text:
# description = new_text + ' No description available'
# else:
# description = 'No description available'
description = 'No description available'
self.description_text.delete('1.0', tk.END)
self.description_text.insert(tk.END, description)
def save_description(self):
image_file = self.image_files[self.current_image_index]
description_file = os.path.splitext(image_file)[0] + '.txt'
description_path = os.path.join(self.folder_path, description_file)
description = self.description_text.get('1.0', tk.END).strip()
with open(description_path, 'w') as f:
f.write(description)
def show_previous_image(self):
if self.current_image_index > 0:
self.current_image_index -= 1
self.show_current_image()
def show_next_image(self):
if self.current_image_index < len(self.image_files) - 1:
self.current_image_index += 1
self.show_current_image()
if __name__ == '__main__':
root = tk.Tk()
folder_path = os.path.dirname(os.path.abspath(__file__))
app = ImageGallery(root, folder_path)
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment