Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from PIL import Image
- from PIL import ImageTk
- import tkinter
- import tkinter.filedialog
- import tkinter.colorchooser
- import cv2
- import matplotlib.pyplot as plt
- import os
- import random
- import numpy as np
- import io
- from tkinter.colorchooser import askcolor
- def is_grey_scale(img_path):
- img = Image.open(img_path).convert('RGB')
- w,h = img.size
- for i in range(w):
- for j in range(h):
- r,g,b = img.getpixel((i,j))
- if r != g != b: return False
- return True
- def selectareImagine():
- global labelA, labelB,labelC,labelD, changedPhoto, pozaInitiala, s, hist, x, valoriHist, poza, lungimePanou, inaltimePanou
- cale = tkinter.filedialog.askopenfilename()
- if not is_grey_scale(cale):
- if len(cale) > 0:
- poza = cv2.imread(cale)
- h, w, _ = poza.shape
- regularizator = 0
- if h > 2000 or w > 2000:
- regularizator = 10
- elif h > 400 and h < 2000 or w > 400 and w < 2000:
- regularizator = 5
- else: regularizator = 2
- lungimePanou = w//regularizator
- inaltimePanou = h//regularizator
- poza = cv2.resize(poza, (lungimePanou, inaltimePanou))
- poza = cv2.cvtColor(poza, cv2.COLOR_BGR2RGB)
- pozaInitiala = poza.copy()
- imageCopy = pozaInitiala.copy()
- changedPhoto = Image.fromarray(imageCopy)
- changedPhoto = changedPhoto.convert('L')
- changedPhotoArray = np.array(changedPhoto)
- hist = cv2.calcHist([changedPhotoArray],[0],None,[256],[0,256])
- x = list(range(0, 256))
- valoriHist = [item for sublist in hist for item in sublist]
- buf = io.BytesIO()
- plt.plot(hist)
- plt.grid(axis='y', alpha=1, linewidth=slider.get())
- plt.grid(axis='x', alpha=1, linewidth=slider.get())
- plt.savefig(buf, format='png')
- im = Image.open(buf)
- im = np.array(im)
- buf.close()
- plt.clf()
- color = ['b','g','r']
- buf = io.BytesIO()
- for i, col in enumerate(color):
- histColor = cv2.calcHist([pozaInitiala],[i],None,[256],[0,256])
- plt.plot(histColor, color = col)
- plt.grid(axis='y', alpha=1, linewidth=slider.get())
- plt.grid(axis='x', alpha=1, linewidth=slider.get())
- plt.savefig(buf, format='png')
- im_color = Image.open(buf)
- im_color = np.array(im_color)
- buf.close()
- plt.clf()
- poza = Image.fromarray(poza)
- poza = ImageTk.PhotoImage(poza)
- poza_hist = Image.fromarray(im)
- poza_hist = ImageTk.PhotoImage(poza_hist)
- poza_hist_color = Image.fromarray(im_color)
- poza_hist_color = ImageTk.PhotoImage(poza_hist_color)
- changedPhoto = ImageTk.PhotoImage(changedPhoto)
- if labelA is None or labelB is None:
- labelA = Label(leftframe,image = poza, width = lungimePanou,height = inaltimePanou)#1
- labelA.image = poza
- labelA.pack(side="top", padx=10, pady=10)
- labelC = Label(rightframe,image=poza_hist_color, width = lungimePanou,height = inaltimePanou)#4
- labelC.image = poza_hist_color
- labelC.pack(side="top", padx=10, pady=10)
- labelB = Label(leftframe,image=changedPhoto, width = lungimePanou,height = inaltimePanou)#2
- labelB.image = changedPhoto
- labelB.pack(side="bottom", padx=10, pady=10)
- labelD = Label(rightframe,image=poza_hist, width = lungimePanou,height = inaltimePanou)#3
- labelD.image = poza_hist
- labelD.pack(side="bottom", padx=10, pady=10)
- else:
- labelA.configure(image = poza)
- labelB.configure(image = changedPhoto)
- labelC.configure(image = poza_hist_color)
- labelD.configure(image = poza_hist)
- labelA.image = poza
- labelB.image = changedPhoto
- labelC.image = poza_hist_color
- labelD.image = poza_hist
- return
- def thickness_grid():
- global labelA, labelB, labelC, labelD, color_code_x_axis, r, g , b, checkVar
- buf = io.BytesIO()
- plt.plot(hist)
- (r, g, b), _ = color_code_y_axis
- plt.grid(axis='y', alpha=checkVar.get(), linewidth=slider.get(),color=(r/255,g/255,b/255,1))
- (r, g, b), _ = color_code_x_axis
- plt.grid(axis='x', alpha=checkVar.get(), linewidth=slider.get(),color=(r/255,g/255,b/255,1))
- plt.savefig(buf, format='png')
- im = Image.open(buf)
- im = np.array(im)
- buf.close()
- plt.clf()
- color = ['b','g','r']
- buf = io.BytesIO()
- for i, col in enumerate(color):
- histColor = cv2.calcHist([pozaInitiala],[i],None,[256],[0,256])
- plt.plot(histColor, color = col)
- (r, g, b), _ = color_code_y_axis
- plt.grid(axis='y', alpha=checkVar.get(), linewidth=slider.get(),color=(r/255,g/255,b/255,1))
- (r, g, b), _ = color_code_x_axis
- plt.grid(axis='x', alpha=checkVar.get(), linewidth=slider.get(),color=(r/255,g/255,b/255,1))
- plt.savefig(buf, format='png')
- im_color = Image.open(buf)
- im_color = np.array(im_color)
- buf.close()
- plt.clf()
- poza_hist = Image.fromarray(im)
- poza_hist = ImageTk.PhotoImage(poza_hist)
- poza_hist_color = Image.fromarray(im_color)
- poza_hist_color = ImageTk.PhotoImage(poza_hist_color)
- if labelA is None or labelB is None:
- labelA = Label(leftframe,image = poza, width = lungimePanou,height = inaltimePanou)#1
- labelA.image = poza
- labelA.pack(side="top", padx=10, pady=10)
- labelC = Label(rightframe,image=poza_hist_color, width = lungimePanou,height = inaltimePanou)#4
- labelC.image = poza_hist_color
- labelC.pack(side="top", padx=10, pady=10)
- labelB = Label(leftframe,image=changedPhoto, width = lungimePanou,height = inaltimePanou)#2
- labelB.image = changedPhoto
- labelB.pack(side="bottom", padx=10, pady=10)
- labelD = Label(rightframe,image=poza_hist, width = lungimePanou,height = inaltimePanou)#3
- labelD.image = poza_hist
- labelD.pack(side="bottom", padx=10, pady=10)
- else:
- labelA.configure(image = poza)
- labelB.configure(image = changedPhoto)
- labelC.configure(image = poza_hist_color)
- labelD.configure(image = poza_hist)
- labelA.image = poza
- labelB.image = changedPhoto
- labelC.image = poza_hist_color
- labelD.image = poza_hist
- return
- def choose_color_input():
- global firstColor, color_code_x_axis, r, g , b
- firstColor = []
- color_code_x_axis = askcolor(title ="Choose color")
- if color_code_x_axis != (None,None):
- if color_y_axis["state"] == "disabled":
- color_y_axis["state"] = "normal"
- def choose_color_y_axis():
- global secondColor , color_code_y_axis, r, g , b
- secondColor = []
- color_code_y_axis = askcolor(title ="Choose color")
- if color_code_y_axis != (None,None):
- if process["state"] == "disabled":
- process["state"] = "normal"
- if grid["state"] == "disabled":
- grid["state"] = "normal"
- def gridOnOff():
- thickness_grid()
- root = Tk()
- root.title('Change Color Bucket')
- labelA = None
- labelB = None
- pozaInitiala = None
- bottomframe = Frame(root)
- bottomframe.pack (side = BOTTOM)
- leftframe = Frame(root)
- leftframe.pack( side = LEFT )
- rightframe = Frame(root)
- rightframe.pack( side = RIGHT)
- btn = Button(bottomframe, text="Upload", command = selectareImagine,height = 2)
- btn.pack(side="bottom", fill="both", expand="no", padx="20")
- color_input = Button(bottomframe, text = "Color input", command = choose_color_input,height = 2, width = 10)
- color_input.pack( side = "bottom", fill = "both",padx="20", pady="10")
- color_y_axis = Button(bottomframe, text = "Color y axis", command = choose_color_y_axis,height = 2, width = 10)
- color_y_axis.pack( side = "bottom", fill = "both",padx="20")
- color_y_axis["state"] = "disabled"
- process = Button(bottomframe, text = "Process", command = thickness_grid ,height = 2, width=10)
- process.pack( side = "bottom", fill = "both",padx="20", pady="10" )
- process["state"] = "disabled"
- slider = Scale(bottomframe, from_=0, to=10, orient=HORIZONTAL)
- slider.set(0)
- slider.pack(side = "bottom", fill = "both",padx="30" )
- checkVar = IntVar()
- grid = Checkbutton(bottomframe, text="Grid on/off" ,height = 2, width = 10, variable = checkVar , command = gridOnOff , onvalue = 1, offvalue = 0)
- grid.pack( side = "bottom", fill = "both", padx = "20")
- grid["state"] = "disabled"
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement