Advertisement
frentzy

histograme python

Nov 15th, 2020
980
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.03 KB | None | 0 0
  1. from tkinter import *
  2. from PIL import Image
  3. from PIL import ImageTk
  4. import tkinter
  5. import tkinter.filedialog
  6. import tkinter.colorchooser
  7. import cv2
  8. import matplotlib.pyplot as plt
  9. import  os
  10. import random
  11. import numpy as np
  12. import io
  13. from tkinter.colorchooser import askcolor  
  14.  
  15. def is_grey_scale(img_path):
  16.     img = Image.open(img_path).convert('RGB')
  17.     w,h = img.size
  18.     for i in range(w):
  19.         for j in range(h):
  20.             r,g,b = img.getpixel((i,j))
  21.             if r != g != b: return False
  22.     return True
  23.  
  24. def selectareImagine():
  25.     global labelA, labelB,labelC,labelD, changedPhoto, pozaInitiala, s, hist, x, valoriHist, poza, lungimePanou, inaltimePanou
  26.     cale = tkinter.filedialog.askopenfilename()
  27.     if not is_grey_scale(cale):
  28.         if len(cale) > 0:
  29.             poza = cv2.imread(cale)
  30.             h, w, _ = poza.shape
  31.             regularizator = 0
  32.             if h > 2000 or w > 2000:
  33.                 regularizator = 10
  34.             elif h > 400 and h < 2000 or w > 400 and w < 2000:
  35.                 regularizator = 5
  36.             else: regularizator = 2
  37.  
  38.             lungimePanou = w//regularizator
  39.             inaltimePanou = h//regularizator
  40.            
  41.             poza = cv2.resize(poza, (lungimePanou, inaltimePanou))
  42.             poza = cv2.cvtColor(poza, cv2.COLOR_BGR2RGB)
  43.            
  44.             pozaInitiala = poza.copy()
  45.             imageCopy = pozaInitiala.copy()
  46.  
  47.            
  48.             changedPhoto = Image.fromarray(imageCopy)
  49.             changedPhoto = changedPhoto.convert('L')
  50.             changedPhotoArray = np.array(changedPhoto)        
  51.            
  52.            
  53.             hist = cv2.calcHist([changedPhotoArray],[0],None,[256],[0,256])
  54.             x = list(range(0, 256))
  55.             valoriHist = [item for sublist in hist for item in sublist]
  56.             buf = io.BytesIO()
  57.             plt.plot(hist)
  58.  
  59.             plt.grid(axis='y', alpha=1, linewidth=slider.get())
  60.             plt.grid(axis='x', alpha=1, linewidth=slider.get())
  61.             plt.savefig(buf, format='png')
  62.             im = Image.open(buf)
  63.             im = np.array(im)
  64.             buf.close()
  65.             plt.clf()
  66.             color = ['b','g','r']
  67.             buf = io.BytesIO()
  68.             for i, col in enumerate(color):
  69.                 histColor = cv2.calcHist([pozaInitiala],[i],None,[256],[0,256])
  70.                 plt.plot(histColor, color = col)
  71.            
  72.  
  73.             plt.grid(axis='y', alpha=1, linewidth=slider.get())
  74.             plt.grid(axis='x', alpha=1, linewidth=slider.get())
  75.             plt.savefig(buf, format='png')
  76.             im_color = Image.open(buf)
  77.             im_color = np.array(im_color)
  78.            
  79.             buf.close()
  80.             plt.clf()
  81.            
  82.            
  83.             poza = Image.fromarray(poza)
  84.             poza = ImageTk.PhotoImage(poza)
  85.             poza_hist = Image.fromarray(im)
  86.             poza_hist = ImageTk.PhotoImage(poza_hist)
  87.             poza_hist_color = Image.fromarray(im_color)
  88.             poza_hist_color = ImageTk.PhotoImage(poza_hist_color)
  89.             changedPhoto = ImageTk.PhotoImage(changedPhoto)
  90.            
  91.            
  92.            
  93.             if labelA is None or labelB is None:
  94.                 labelA = Label(leftframe,image = poza, width = lungimePanou,height = inaltimePanou)#1
  95.                 labelA.image = poza
  96.                 labelA.pack(side="top", padx=10, pady=10)
  97.                 labelC = Label(rightframe,image=poza_hist_color, width = lungimePanou,height = inaltimePanou)#4
  98.                 labelC.image = poza_hist_color
  99.                 labelC.pack(side="top", padx=10, pady=10)
  100.                 labelB = Label(leftframe,image=changedPhoto, width = lungimePanou,height = inaltimePanou)#2
  101.                 labelB.image = changedPhoto
  102.                 labelB.pack(side="bottom", padx=10, pady=10)
  103.                 labelD = Label(rightframe,image=poza_hist, width = lungimePanou,height = inaltimePanou)#3
  104.                 labelD.image = poza_hist
  105.                 labelD.pack(side="bottom", padx=10, pady=10)
  106.                
  107.             else:
  108.                 labelA.configure(image = poza)
  109.                 labelB.configure(image = changedPhoto)
  110.                 labelC.configure(image = poza_hist_color)
  111.                 labelD.configure(image = poza_hist)
  112.                 labelA.image = poza
  113.                 labelB.image = changedPhoto
  114.                 labelC.image = poza_hist_color
  115.                 labelD.image = poza_hist
  116.     return
  117.  
  118. def thickness_grid():
  119.     global labelA, labelB, labelC, labelD, color_code_x_axis, r, g , b, checkVar
  120.    
  121.  
  122.     buf = io.BytesIO()
  123.  
  124.     plt.plot(hist)
  125.     (r, g, b), _ = color_code_y_axis
  126.     plt.grid(axis='y', alpha=checkVar.get(), linewidth=slider.get(),color=(r/255,g/255,b/255,1))
  127.     (r, g, b), _ = color_code_x_axis
  128.     plt.grid(axis='x', alpha=checkVar.get(), linewidth=slider.get(),color=(r/255,g/255,b/255,1))
  129.     plt.savefig(buf, format='png')
  130.     im = Image.open(buf)
  131.     im = np.array(im)
  132.     buf.close()
  133.     plt.clf()
  134.    
  135.     color = ['b','g','r']
  136.     buf = io.BytesIO()
  137.     for i, col in enumerate(color):
  138.         histColor = cv2.calcHist([pozaInitiala],[i],None,[256],[0,256])
  139.         plt.plot(histColor, color = col)
  140.  
  141.  
  142.     (r, g, b), _ = color_code_y_axis
  143.     plt.grid(axis='y', alpha=checkVar.get(), linewidth=slider.get(),color=(r/255,g/255,b/255,1))
  144.     (r, g, b), _ = color_code_x_axis
  145.     plt.grid(axis='x', alpha=checkVar.get(), linewidth=slider.get(),color=(r/255,g/255,b/255,1))
  146.     plt.savefig(buf, format='png')
  147.     im_color = Image.open(buf)
  148.     im_color = np.array(im_color)
  149.  
  150.     buf.close()
  151.     plt.clf()
  152.    
  153.     poza_hist = Image.fromarray(im)
  154.     poza_hist = ImageTk.PhotoImage(poza_hist)
  155.     poza_hist_color = Image.fromarray(im_color)
  156.     poza_hist_color = ImageTk.PhotoImage(poza_hist_color)
  157.    
  158.  
  159.     if labelA is None or labelB is None:
  160.         labelA = Label(leftframe,image = poza, width = lungimePanou,height = inaltimePanou)#1
  161.         labelA.image = poza
  162.         labelA.pack(side="top", padx=10, pady=10)
  163.         labelC = Label(rightframe,image=poza_hist_color, width = lungimePanou,height = inaltimePanou)#4
  164.         labelC.image = poza_hist_color
  165.         labelC.pack(side="top", padx=10, pady=10)
  166.         labelB = Label(leftframe,image=changedPhoto, width = lungimePanou,height = inaltimePanou)#2
  167.         labelB.image = changedPhoto
  168.         labelB.pack(side="bottom", padx=10, pady=10)
  169.         labelD = Label(rightframe,image=poza_hist, width = lungimePanou,height = inaltimePanou)#3
  170.         labelD.image = poza_hist
  171.         labelD.pack(side="bottom", padx=10, pady=10)
  172.  
  173.     else:
  174.         labelA.configure(image = poza)
  175.         labelB.configure(image = changedPhoto)
  176.         labelC.configure(image = poza_hist_color)
  177.         labelD.configure(image = poza_hist)
  178.         labelA.image = poza
  179.         labelB.image = changedPhoto
  180.         labelC.image = poza_hist_color
  181.         labelD.image = poza_hist
  182.     return
  183.  
  184. def choose_color_input():
  185.     global firstColor, color_code_x_axis, r, g , b
  186.     firstColor = []
  187.     color_code_x_axis = askcolor(title ="Choose color")  
  188.     if color_code_x_axis != (None,None):
  189.         if color_y_axis["state"] == "disabled":
  190.             color_y_axis["state"] = "normal"
  191.    
  192. def choose_color_y_axis():
  193.     global secondColor , color_code_y_axis, r, g , b
  194.     secondColor = []
  195.     color_code_y_axis = askcolor(title ="Choose color")  
  196.     if color_code_y_axis != (None,None):
  197.         if process["state"] == "disabled":
  198.             process["state"] = "normal"
  199.         if grid["state"] == "disabled":
  200.             grid["state"] = "normal"
  201.        
  202.  
  203. def gridOnOff():
  204.     thickness_grid()
  205.    
  206. root = Tk()
  207. root.title('Change Color Bucket')
  208. labelA = None
  209. labelB = None
  210. pozaInitiala = None
  211.  
  212. bottomframe = Frame(root)
  213. bottomframe.pack (side = BOTTOM)
  214.  
  215. leftframe = Frame(root)
  216. leftframe.pack( side = LEFT )
  217. rightframe = Frame(root)
  218. rightframe.pack( side = RIGHT)
  219.  
  220.  
  221. btn = Button(bottomframe, text="Upload", command = selectareImagine,height = 2)
  222. btn.pack(side="bottom", fill="both", expand="no", padx="20")
  223.  
  224. color_input = Button(bottomframe, text = "Color input", command = choose_color_input,height = 2, width = 10)
  225. color_input.pack( side = "bottom", fill = "both",padx="20", pady="10")
  226.  
  227. color_y_axis = Button(bottomframe, text = "Color y axis", command = choose_color_y_axis,height = 2, width = 10)
  228. color_y_axis.pack( side = "bottom", fill = "both",padx="20")
  229. color_y_axis["state"] = "disabled"
  230.  
  231. process = Button(bottomframe, text = "Process", command = thickness_grid ,height = 2, width=10)
  232. process.pack( side = "bottom", fill = "both",padx="20", pady="10" )
  233. process["state"] = "disabled"
  234.  
  235. slider = Scale(bottomframe, from_=0, to=10, orient=HORIZONTAL)
  236. slider.set(0)
  237. slider.pack(side = "bottom", fill = "both",padx="30" )
  238.  
  239.  
  240. checkVar = IntVar()
  241. grid = Checkbutton(bottomframe, text="Grid on/off" ,height = 2, width = 10, variable = checkVar , command = gridOnOff , onvalue = 1, offvalue = 0)
  242. grid.pack( side = "bottom", fill = "both", padx = "20")
  243. grid["state"] = "disabled"
  244. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement