#drawing/upload GUI: a dialogue window that allows upload of .png file or an image from interactive canvas which can be downloaded. import tkinter as tk from PIL import Image from tkinter import filedialog # Initialize the main window root = tk.Tk() root.title("Flick The Wand") root.geometry("600x600") root.configure(bg="white") # Title Label title_label = tk.Label( root, text="FLICK THE WAND✨", font=("Arial", 18, "bold"), bg="white" ) title_label.pack(pady=20) subtitle_label = tk.Label( root, text="Generate a portrait.\nYou can upload from your computer or draw your own.", font=("Arial", 12), bg="white", fg="gray", justify="center", ) subtitle_label.pack(pady=10) # Upload Button Functionality def upload_file(): file_path = filedialog.askopenfilename( title="Select a File", filetypes=[("Image Files", "*.png;*.jpg;*.jpeg;*.bmp;*.gif")], ) if file_path: print(f"File selected: {file_path}") # Upload Button upload_button = tk.Button( root, text="UPLOAD⬆", font=("Arial", 14, "bold"), bg="black", fg="white", command=upload_file, relief="flat", width=20, ) upload_button.pack(pady=10) # Separator Label separator_label = tk.Label(root, text="or continue with", font=("Arial", 12), bg="white") separator_label.pack(pady=5) # Draw a Portrait Button Functionality def draw_portrait(): draw_window = tk.Toplevel(root) draw_window.title("Draw a Portrait") draw_window.geometry("600x600") draw_window.configure(bg="white") canvas = tk.Canvas(draw_window, width=600, height=600, bg="white") canvas.pack(fill=tk.BOTH, expand=True, padx=10, pady=10) drawing = False last_x, last_y = 0, 0 def start_drawing(event): nonlocal drawing, last_x, last_y drawing = True last_x, last_y = event.x, event.y def draw(event): nonlocal drawing, last_x, last_y if drawing: canvas.create_line(last_x, last_y, event.x, event.y, width=2, fill="black", capstyle=tk.ROUND, smooth=True) last_x, last_y = event.x, event.y def stop_drawing(event): nonlocal drawing drawing = False canvas.bind("", start_drawing) canvas.bind("", draw) canvas.bind("", stop_drawing) # Finish Button Functionality def finish_drawing(): # Save the drawing content as a .eps file eps_filename = "drawing.eps" canvas.postscript(file=eps_filename, colormode='color') # Convert .eps to .png using Pillow img = Image.open(eps_filename) save_path = filedialog.asksaveasfilename( defaultextension=".png", filetypes=[("PNG files", "*.png"), ("All files", "*.*")], title="Save Drawing As" ) if save_path: img.save(save_path) print(f"File saved as: {save_path}") draw_window.destroy() def place_finish_button(): finish_button = tk.Button( draw_window, text="Finish", font=("Arial", 12, "bold"), bg="black", fg="white", command=finish_drawing, relief="flat", width=15, ) finish_button.place(x=draw_window.winfo_width() - 10, y=draw_window.winfo_height() - 50, anchor="se") # Delay button placement until window is fully loaded draw_window.after(100, place_finish_button) # Draw a Portrait Button draw_button = tk.Button( root, text="DRAW A PORTRAIT🎨", font=("Arial", 14, "bold"), bg="black", fg="white", command=draw_portrait, relief="flat", width=20, ) draw_button.pack(pady=10) # Create the first label with the "H" text label_hello = tk.Label(root, text="HARVARD SHOP", font=("Arial", 8), fg="#A51C30", bg="white") label_hello.pack(side="bottom") # Create the second label with the "HTMAA" text label_htmaa = tk.Label(root, text="HTMAA", font=("Arial", 14, "bold"), fg="black", bg="white") label_htmaa.pack(side="bottom", pady=10) # Start the Tkinter main loop root.mainloop()