|
| 1 | +import customtkinter as ctk |
| 2 | +import matplotlib |
| 3 | +from customtkinter import filedialog |
| 4 | +import matplotlib.pyplot as plt |
| 5 | +from colorthief import ColorThief |
| 6 | + |
| 7 | +matplotlib.use('TkAgg') |
| 8 | +ctk.set_appearance_mode("dark") |
| 9 | +ctk.set_default_color_theme("dark-blue") |
| 10 | + |
| 11 | + |
| 12 | +# Function to select the image for color extraction |
| 13 | +def select_image(): |
| 14 | + try: |
| 15 | + global selected_image |
| 16 | + selected_image = filedialog.askopenfilename(filetypes=[("Image Files", "*.jpg;*.jpeg;*.png")]) |
| 17 | + ct = ColorThief(selected_image) |
| 18 | + dominant_color = ct.get_color(quality=1) |
| 19 | + status_label.configure(text="RGB: " + str(dominant_color)) |
| 20 | + plt.imshow([[dominant_color]]) |
| 21 | + plt.show() |
| 22 | + except Exception as e: |
| 23 | + print(f"Error: {str(e)}") |
| 24 | + |
| 25 | + |
| 26 | +# Global variable to store the selected image path |
| 27 | +selected_image = None |
| 28 | + |
| 29 | +# Create the main customtkinter window |
| 30 | +root = ctk.CTk() |
| 31 | +root.title("Dominant Color Extractor") |
| 32 | +root.geometry("375x130") |
| 33 | +root.resizable(False, False) |
| 34 | + |
| 35 | +frame = ctk.CTkFrame(master=root) |
| 36 | +frame.pack(pady=10, padx=10, fill="both", expand=True) |
| 37 | + |
| 38 | +select_button = ctk.CTkButton(master=frame, text="Select Image", command=select_image) |
| 39 | +select_button.pack(pady=20, padx=10) |
| 40 | + |
| 41 | +status_label = ctk.CTkLabel(master=frame, text="Please select an image.") |
| 42 | +status_label.pack(pady=5, padx=10) |
| 43 | + |
| 44 | + |
| 45 | +root.mainloop() |
0 commit comments