|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Created on Thu Apr 4 23:33:02 2024 |
| 4 | +
|
| 5 | +@author: Anas Odeh |
| 6 | +""" |
| 7 | + |
| 8 | +from pathlib import Path |
| 9 | +from tkinter import Tk, Canvas, Button, PhotoImage, messagebox |
| 10 | +from tkinter.ttk import Progressbar |
| 11 | +import os |
| 12 | +import shutil |
| 13 | +import easygui |
| 14 | +import threading |
| 15 | + |
| 16 | +OUTPUT_PATH = Path(__file__).parent |
| 17 | +ASSETS_PATH = OUTPUT_PATH / Path(r"C:\Users\Home\Desktop\Anas Odeh\computional biology\build\assets\frame0") |
| 18 | + |
| 19 | +def relative_to_assets(path: str) -> Path: |
| 20 | + return ASSETS_PATH / Path(path) |
| 21 | + |
| 22 | +def update_progress(value): |
| 23 | + # Update the progress bar's value |
| 24 | + progress_bar['value'] = value |
| 25 | + # Update the text above the progress bar to show current progress percentage |
| 26 | + progress_text = f"Progress: {int(value)}%" |
| 27 | + canvas.itemconfig(progress_label, text=progress_text) |
| 28 | + window.update_idletasks() |
| 29 | + |
| 30 | +def complete_progress(): |
| 31 | + messagebox.showinfo("Success", "Images have been successfully renamed and pooled.") |
| 32 | + # Reset progress bar and label when done |
| 33 | + progress_bar['value'] = 0 |
| 34 | + canvas.itemconfig(progress_label, text="Progress: 0%") |
| 35 | + |
| 36 | +def process_images(): |
| 37 | + base_directory = input_folder |
| 38 | + output_directory = os.path.join(base_directory, 'pooled_images') |
| 39 | + |
| 40 | + if not os.path.exists(output_directory): |
| 41 | + os.makedirs(output_directory) |
| 42 | + |
| 43 | + items = [item for item in os.listdir(base_directory) if os.path.isdir(os.path.join(base_directory, item)) and item != 'pooled_images'] |
| 44 | + total_items = len(items) |
| 45 | + |
| 46 | + for index, item in enumerate(items, start=1): |
| 47 | + item_path = os.path.join(base_directory, item) |
| 48 | + for file in os.listdir(item_path): |
| 49 | + if file.lower().endswith(('.png', '.jpg', '.jpeg', '.tif')): |
| 50 | + old_file_path = os.path.join(item_path, file) |
| 51 | + new_file_name = f"{item}{os.path.splitext(file)[1]}" |
| 52 | + new_file_path_in_subfolder = os.path.join(item_path, new_file_name) |
| 53 | + new_file_path_in_output = os.path.join(output_directory, new_file_name) |
| 54 | + |
| 55 | + # Rename within the subfolder |
| 56 | + if not os.path.exists(new_file_path_in_subfolder): |
| 57 | + shutil.move(old_file_path, new_file_path_in_subfolder) |
| 58 | + |
| 59 | + # Copy to the pooled_images directory |
| 60 | + if not os.path.exists(new_file_path_in_output): |
| 61 | + shutil.copy2(new_file_path_in_subfolder, new_file_path_in_output) |
| 62 | + break |
| 63 | + |
| 64 | + progress = (index / total_items) * 100 |
| 65 | + window.after(100, update_progress, progress) |
| 66 | + |
| 67 | + window.after(100, complete_progress) |
| 68 | + |
| 69 | +def start_image_processing_thread(): |
| 70 | + if 'input_folder' not in globals(): |
| 71 | + messagebox.showerror("Error", "Please select your input folder first!") |
| 72 | + return |
| 73 | + |
| 74 | + # Start the image processing in a separate thread to avoid UI freezing |
| 75 | + threading.Thread(target=process_images, daemon=True).start() |
| 76 | + |
| 77 | +def select_input_folder(): |
| 78 | + folder = easygui.diropenbox() |
| 79 | + if folder: |
| 80 | + global input_folder |
| 81 | + input_folder = folder |
| 82 | + |
| 83 | +window = Tk() |
| 84 | +window.geometry("1440x1024") |
| 85 | +window.configure(bg="#FFFFFF") |
| 86 | + |
| 87 | +canvas = Canvas(window, bg="#FFFFFF", height=1024, width=1440, bd=0, highlightthickness=0, relief="ridge") |
| 88 | +canvas.place(x=0, y=0) |
| 89 | + |
| 90 | +image_image_1 = PhotoImage(file=relative_to_assets("image_1.png")) |
| 91 | +image_1 = canvas.create_image(719.0, 512.0, image=image_image_1) |
| 92 | + |
| 93 | +canvas.create_text(60.0, 964.0, anchor="nw", text="©️By using this application, you must cite the name of the app developer, Anas Odeh", fill="#FFFFFF", font=("Inter Bold", 32 * -1)) |
| 94 | + |
| 95 | +image_image_2 = PhotoImage(file=relative_to_assets("image_2.png")) |
| 96 | +image_2 = canvas.create_image(735.0, 55.0, image=image_image_2) |
| 97 | + |
| 98 | +button_image_1 = PhotoImage(file=relative_to_assets("button_1.png")) |
| 99 | +button_1 = Button(image=button_image_1, borderwidth=0, highlightthickness=0, command=select_input_folder, relief="flat") |
| 100 | +button_1.place(x=446.0, y=236.0, width=603.0, height=45.0) |
| 101 | + |
| 102 | +button_image_2 = PhotoImage(file=relative_to_assets("button_2.png")) |
| 103 | +button_2 = Button(image=button_image_2, borderwidth=0, highlightthickness=0, command=start_image_processing_thread, relief="flat") |
| 104 | +button_2.place(x=594.0, y=317.0, width=283.0, height=41.0) |
| 105 | + |
| 106 | +# Create and place the progress label and bar |
| 107 | +progress_label = canvas.create_text(720, 400, text="Progress: 0%", fill="white", font=("Helvetica", 24)) |
| 108 | +progress_bar = Progressbar(canvas, orient='horizontal', length=1000, mode='determinate') |
| 109 | +progress_bar.place(x=220, y=425) |
| 110 | + |
| 111 | +image_image_3 = PhotoImage(file=relative_to_assets("image_3.png")) |
| 112 | +image_3 = canvas.create_image(712.0, 150.0, image=image_image_3) |
| 113 | + |
| 114 | +window.resizable(False, False) |
| 115 | +window.mainloop() |
0 commit comments