|
| 1 | +from tkinter import * |
| 2 | +from tkinter import filedialog |
| 3 | +import numpy as np |
| 4 | +from PIL import Image |
| 5 | +from scipy.spatial import KDTree |
| 6 | +from webcolors import ( |
| 7 | + CSS3_HEX_TO_NAMES, |
| 8 | + hex_to_rgb, |
| 9 | +) |
| 10 | + |
| 11 | +NUMBER_OF_COLORS = 15 |
| 12 | +TITLE_FONT = "Arial" |
| 13 | +TITLE_COLOR = "#EFEFEF" |
| 14 | +WORD_FONT = "Times New Roman" |
| 15 | +WORD_COLOR = "#EEEEEE" |
| 16 | +BACKGROUND_COLOR = "#222832" |
| 17 | +BTN_BACKGROUND_COLOR = "#2F3847" |
| 18 | + |
| 19 | + |
| 20 | +def get_img_colors(): |
| 21 | + img_file = filedialog.askopenfilename( |
| 22 | + initialdir="/", |
| 23 | + title="Select IMG", |
| 24 | + filetypes=(('Image Files', "*.jpg;*.jpeg;*.png"), ("All Files", "*.*")) |
| 25 | + ) |
| 26 | + img = Image.open(img_file, 'r').convert('RGB') |
| 27 | + color_codes = palette(img) |
| 28 | + color_names = [] |
| 29 | + for code in color_codes: |
| 30 | + if convert_rgb_to_names(code) not in color_names and len(color_names) < NUMBER_OF_COLORS: |
| 31 | + color_names.append(convert_rgb_to_names(code)) |
| 32 | + colors_text.insert(END, convert_rgb_to_names(code) + '\n') |
| 33 | + |
| 34 | + |
| 35 | +def palette(img): |
| 36 | + """ |
| 37 | + Return palette in descending order of frequency |
| 38 | + """ |
| 39 | + arr = np.asarray(img) |
| 40 | + palette, index = np.unique(asvoid(arr).ravel(), return_inverse=True) |
| 41 | + palette = palette.view(arr.dtype).reshape(-1, arr.shape[-1]) |
| 42 | + count = np.bincount(index) |
| 43 | + order = np.argsort(count) |
| 44 | + return palette[order[::-1]] |
| 45 | + |
| 46 | + |
| 47 | +def asvoid(arr): |
| 48 | + """View the array as dtype np.void (bytes) |
| 49 | + This collapses ND-arrays to 1D-arrays, so you can perform 1D operations on them. |
| 50 | + http://stackoverflow.com/a/16216866/190597 (Jaime) |
| 51 | + http://stackoverflow.com/a/16840350/190597 (Jaime) |
| 52 | + Warning: |
| 53 | + >>> asvoid([-0.]) == asvoid([0.]) |
| 54 | + array([False], dtype=bool) |
| 55 | + """ |
| 56 | + arr = np.ascontiguousarray(arr) |
| 57 | + return arr.view(np.dtype((np.void, arr.dtype.itemsize * arr.shape[-1]))) |
| 58 | + |
| 59 | + |
| 60 | +def convert_rgb_to_names(rgb_tuple): |
| 61 | + # A dictionary of all the hex and their respective names in css3 |
| 62 | + css3_db = CSS3_HEX_TO_NAMES |
| 63 | + names = [] |
| 64 | + rgb_values = [] |
| 65 | + for color_hex, color_name in css3_db.items(): |
| 66 | + names.append(color_name) |
| 67 | + rgb_values.append(hex_to_rgb(color_hex)) |
| 68 | + |
| 69 | + kdt_db = KDTree(rgb_values) |
| 70 | + distance, index = kdt_db.query(rgb_tuple) |
| 71 | + return names[index].title() |
| 72 | + |
| 73 | + |
| 74 | +def copy_to_clipboard(): |
| 75 | + root.clipboard_append(colors_text.get("1.0", END)) |
| 76 | + |
| 77 | + |
| 78 | +def clear_text(): |
| 79 | + colors_text.delete("1.0", END) |
| 80 | + |
| 81 | + |
| 82 | +root = Tk() |
| 83 | +root.title('Image to Color List') |
| 84 | +root.config(padx=25, pady=25, bg=BACKGROUND_COLOR) |
| 85 | +title_label = Label(text="Image to Color List", font=(TITLE_FONT, 32, "bold"), fg=TITLE_COLOR, bg=BACKGROUND_COLOR) |
| 86 | +open_btn = Button(text="Select Image", font=(WORD_FONT, 20), width=25, fg=WORD_COLOR, bg=BTN_BACKGROUND_COLOR, |
| 87 | + command=get_img_colors) |
| 88 | +copy_btn = Button(text="Copy Text", font=(WORD_FONT, 20), width=25, fg=WORD_COLOR, bg=BTN_BACKGROUND_COLOR, |
| 89 | + command=copy_to_clipboard) |
| 90 | +clear_btn = Button(text="Clear Text", font=(WORD_FONT, 20), width=25, fg=WORD_COLOR, bg=BTN_BACKGROUND_COLOR, |
| 91 | + command=clear_text) |
| 92 | +colors_text = Text(root, width=35, height=16, font=(TITLE_FONT, 14)) |
| 93 | +title_label.grid(column=0, row=0, pady=20) |
| 94 | +colors_text.grid(column=0, row=1) |
| 95 | +open_btn.grid(column=0, row=2, pady=10) |
| 96 | +copy_btn.grid(column=0, row=3) |
| 97 | +clear_btn.grid(column=0, row=4, pady=10) |
| 98 | +root.mainloop() |
0 commit comments