Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.12 KB | None | 0 0
  1. # import the necessary packages
  2. from tkinter import *
  3. from PIL import Image
  4. from PIL import ImageTk
  5. from PIL import ImageFilter
  6. from tkinter import filedialog
  7. from matplotlib import pyplot as plt
  8. from scipy import ndimage
  9. #import tkFileDialog
  10. import cv2
  11. import time
  12. import numpy
  13.  
  14. global gray
  15. global image
  16. global panelA
  17.  
  18. def select_image():
  19.     # grab a reference to the image panels
  20.     global image
  21.     # open a file chooser dialog and allow the user to select an input
  22.     # image
  23.     #path = tkFileDialog.askopenfilename()
  24.     #root = Tk()
  25.     #root.
  26.     filename =  filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
  27.     print (filename)
  28.     # ensure a file path was selected
  29.     if len(filename) > 0:
  30.         # load the image from disk, convert it to grayscale, and detect
  31.         # edges in it
  32.         image = cv2.imread(filename)
  33.        
  34.         # check()
  35.         # time.sleep(5)
  36.         hpf()
  37.         return image
  38.         # edged = cv2.Canny(gray, 50, 100)
  39.         # # OpenCV represents images in BGR order; however PIL represents
  40.         # # images in RGB order, so we need to swap the channels
  41.         # image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  42.         # convert the images to PIL format...
  43.         # image = Image.fromarray(image)
  44.         # edged = Image.fromarray(edged)
  45.         # # ...and then to ImageTk format
  46.         # image = ImageTk.PhotoImage(image)
  47.         # edged = ImageTk.PhotoImage(edged)
  48.         # # if the panels are None, initialize them
  49.         # if panelA is None: #or panelB is None:
  50.         #     # the first panel will store our original image
  51.         #     panelA = Label(image=image)
  52.         #     panelA.image = image
  53.         #     panelA.pack(side="left", padx=10, pady=10)
  54.         #     # while the second panel will store the edge map
  55.         #     # panelB = Label(image=edged)
  56.         #     # panelB.image = edged
  57.         #     # panelB.pack(side="right", padx=10, pady=10)
  58.         # # otherwise, update the image panels
  59.         # else:
  60.         #     # update the pannels
  61.         #     panelA.configure(image=image)
  62.         #     #panelB.configure(image=edged)
  63.         #     panelA.image = image
  64.         #     #panelB.image = edged
  65. # initialize the window toolkit along with the two image pane
  66.        
  67. def check():
  68.     global panelA, gray, image
  69.    
  70.     gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  71.     #image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  72.     image = Image.fromarray(gray)
  73.     image = ImageTk.PhotoImage(image)
  74.     if panelA is None:
  75.         panelA = Label(image=image)
  76.         panelA.image = image
  77.         panelA.pack(side="left", padx=10, pady=10)
  78.     else:
  79.         panelA.configure(image=image)
  80.         panelA.image = image
  81.    
  82.  
  83. def hpf():
  84.     global panelA, image, filtered
  85.     gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
  86.     #image2 = Image.fromarray(gray)
  87.     #dane = numpy.array(image, dtype=float)
  88.     #low = ndimage.gaussian_filter(dane, 5)
  89.     #hp = dane - low
  90.     #blur = image2.filter(ImageFilter.GaussianBlur(radius = 20))
  91.     blur = cv2.GaussianBlur(gray,(5,5),0)
  92.     hp = gray - blur
  93.     image2 = Image.fromarray(hp)
  94.     image2 = ImageTk.PhotoImage(image2)
  95.     #laplacian = cv2.Laplacian(gray,cv2.CV_64F)
  96.    
  97.     #filtered = cv2.subtract(img, blur)
  98.     #filtered = filtered + 127*numpy.ones(neg_frame.shape, numpy.uint8)  
  99.     if panelA is None:
  100.         panelA = Label(image=image2)
  101.         panelA.image = image2
  102.         panelA.pack(side="left", padx=10, pady=10)
  103.     else:
  104.         panelA.configure(image=image2)
  105.         panelA.image = image2
  106.  
  107.  
  108.        
  109. root = Tk()
  110. panelA = None
  111. #panelB = None
  112. # create a button, then when pressed, will trigger a file chooser
  113. # dialog and allow the user to select an input image; then add the
  114. # button the GUI
  115. btn = Button(root, text="Select an image", command=select_image)
  116. btn.pack(side="bottom", fill="both", expand="yes", padx="10", pady="10")
  117. # btn = Button(root, text="Check", command=check)
  118. # btn.pack(side="bottom", fill="both", expand="yes", padx="10", pady="30")
  119. # kick off the GUI
  120. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement