|
| 1 | +import numpy as np |
| 2 | +import cv2 |
| 3 | +from tkinter import Tk, END, SUNKEN, Label, Button, Frame |
| 4 | +from tkinter import font, filedialog, X, Text, BOTH |
| 5 | +from PIL import ImageTk, Image |
| 6 | +import os |
| 7 | + |
| 8 | +cwd = os.path.dirname(os.path.realpath(__file__)) |
| 9 | + |
| 10 | + |
| 11 | +class AlColorImageGenerator: |
| 12 | + def __init__(self): |
| 13 | + root = Tk(className="ALCOLORIMAGENERATOR") |
| 14 | + root.geometry("400x150+1510+865") |
| 15 | + root.resizable(0, 0) |
| 16 | + root.iconbitmap(os.path.join(cwd + '\\UI\\icons', |
| 17 | + 'alcolorimagegenerator.ico')) |
| 18 | + root.config(bg="#000000") |
| 19 | + root.overrideredirect(1) |
| 20 | + color = '#000000' |
| 21 | + |
| 22 | + def callback(event): |
| 23 | + root.geometry("400x130+1510+885") |
| 24 | + |
| 25 | + def showScreen(event): |
| 26 | + root.deiconify() |
| 27 | + root.overrideredirect(1) |
| 28 | + |
| 29 | + def screenAppear(event): |
| 30 | + root.overrideredirect(1) |
| 31 | + |
| 32 | + def hideScreen(): |
| 33 | + root.overrideredirect(0) |
| 34 | + root.iconify() |
| 35 | + |
| 36 | + def openImage(): |
| 37 | + imageFileEntry.delete(1.0, END) |
| 38 | + filename = filedialog.askopenfilename(filetypes=[('Image Files', |
| 39 | + '*.jpg *.jpeg ' |
| 40 | + '*.bmp *.png ' |
| 41 | + '*.webp *.tiff')] |
| 42 | + ) |
| 43 | + imageFileEntry.insert(1.0, filename) |
| 44 | + |
| 45 | + def convert(): |
| 46 | + net = cv2.dnn.readNetFromCaffe(cwd + '\\AlColorImageGenerator\\mod' |
| 47 | + 'el\\colorization_deploy_v2.prototx' |
| 48 | + 't', cwd + '\\AlColorImageGenerator' |
| 49 | + '\\model\\colorization_release_v2.' |
| 50 | + 'caffemodel') |
| 51 | + pts = np.load(cwd + '\\AlColorImageGenerator\\model\\pts_in_hull.' |
| 52 | + 'npy') |
| 53 | + class8 = net.getLayerId("class8_ab") |
| 54 | + conv8 = net.getLayerId("conv8_313_rh") |
| 55 | + pts = pts.transpose().reshape(2, 313, 1, 1) |
| 56 | + net.getLayer(class8).blobs = [pts.astype("float32")] |
| 57 | + net.getLayer(conv8).blobs = [np.full([1, 313], 2.606, dtype='float' |
| 58 | + '32')] |
| 59 | + img = imageFileEntry.get("1.0", END) |
| 60 | + img = img.replace('/', '\\')[:-1] |
| 61 | + nimg = os.path.basename(img) |
| 62 | + cimg = 'color_' + nimg |
| 63 | + image = cv2.imread(img) |
| 64 | + scaled = image.astype("float32") / 255.0 |
| 65 | + lab = cv2.cvtColor(scaled, cv2.COLOR_BGR2LAB) |
| 66 | + resized = cv2.resize(lab, (224, 224)) |
| 67 | + L = cv2.split(resized)[0] |
| 68 | + L -= 50 |
| 69 | + net.setInput(cv2.dnn.blobFromImage(L)) |
| 70 | + ab = net.forward()[0, :, :, :].transpose((1, 2, 0)) |
| 71 | + ab = cv2.resize(ab, (image.shape[1], image.shape[0])) |
| 72 | + L = cv2.split(lab)[0] |
| 73 | + colorized = np.concatenate((L[:, :, np.newaxis], ab), axis=2) |
| 74 | + colorized = cv2.cvtColor(colorized, cv2.COLOR_LAB2BGR) |
| 75 | + colorized = np.clip(colorized, 0, 1) |
| 76 | + colorized = (255 * colorized).astype("uint8") |
| 77 | + cimg = os.path.join(cwd + '\\AlColorImageGenerator\\images\\color', |
| 78 | + cimg) |
| 79 | + cv2.imwrite(cimg, colorized) |
| 80 | + cv2.imshow("Original", image) |
| 81 | + cv2.imshow("Colorized", colorized) |
| 82 | + cv2.waitKey(0) |
| 83 | + |
| 84 | + textHighlightFont = font.Font(family='OnePlus Sans Display', size=12) |
| 85 | + appHighlightFont = font.Font(family='OnePlus Sans Display', size=12, |
| 86 | + weight='bold') |
| 87 | + |
| 88 | + titleBar = Frame(root, bg='#141414', relief=SUNKEN, bd=0) |
| 89 | + icon = Image.open(os.path.join(cwd + '\\UI\\icons', |
| 90 | + 'alcolorimagegenerator.ico')) |
| 91 | + icon = icon.resize((30, 30), Image.ANTIALIAS) |
| 92 | + icon = ImageTk.PhotoImage(icon) |
| 93 | + iconLabel = Label(titleBar, image=icon) |
| 94 | + iconLabel.photo = icon |
| 95 | + iconLabel.config(bg='#141414') |
| 96 | + iconLabel.grid(row=0, column=0, sticky="nsew") |
| 97 | + titleLabel = Label(titleBar, text='ALCOLORIMAGEGENERATOR', |
| 98 | + fg='#909090', bg='#141414', font=appHighlightFont) |
| 99 | + titleLabel.grid(row=0, column=1, sticky="nsew") |
| 100 | + closeButton = Button(titleBar, text="x", bg='#141414', fg="#909090", |
| 101 | + borderwidth=0, command=root.destroy, |
| 102 | + font=appHighlightFont) |
| 103 | + closeButton.grid(row=0, column=3, sticky="nsew") |
| 104 | + minimizeButton = Button(titleBar, text="-", bg='#141414', fg="#909090", |
| 105 | + borderwidth=0, command=hideScreen, |
| 106 | + font=appHighlightFont) |
| 107 | + minimizeButton.grid(row=0, column=2, sticky="nsew") |
| 108 | + titleBar.grid_columnconfigure(0, weight=1) |
| 109 | + titleBar.grid_columnconfigure(1, weight=20) |
| 110 | + titleBar.grid_columnconfigure(2, weight=1) |
| 111 | + titleBar.grid_columnconfigure(3, weight=1) |
| 112 | + titleBar.pack(fill=X) |
| 113 | + |
| 114 | + imageFile = Button(root, text="IMAGE TO BE COVERTED", borderwidth=0, |
| 115 | + highlightthickness=3, command=openImage) |
| 116 | + imageFile.pack(fill=X) |
| 117 | + imageFile.config(bg=color, fg="white", font=appHighlightFont) |
| 118 | + imageFileEntry = Text(root, bg="white", fg=color, |
| 119 | + highlightbackground=color, highlightcolor=color, |
| 120 | + highlightthickness=3, bd=0, |
| 121 | + font=textHighlightFont, height=1) |
| 122 | + imageFileEntry.pack(fill=BOTH, expand=True) |
| 123 | + |
| 124 | + convert = Button(root, borderwidth=0, highlightthickness=5, |
| 125 | + text="CONVERT IMAGE", command=convert) |
| 126 | + convert.config(bg=color, fg="white", font=appHighlightFont) |
| 127 | + convert.pack(fill=X) |
| 128 | + |
| 129 | + titleBar.bind("<B1-Motion>", callback) |
| 130 | + titleBar.bind("<Button-3>", showScreen) |
| 131 | + titleBar.bind("<Map>", screenAppear) |
| 132 | + |
| 133 | + root.mainloop() |
| 134 | + |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + AlColorImageGenerator() |
0 commit comments