Shcoder001

photo edit

Mar 10th, 2023
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | None | 0 0
  1. from tkinter import *
  2. from tkinter import filedialog
  3.  
  4. import PIL
  5. import matplotlib.pyplot as plt
  6. import cv2
  7. from PIL import ImageTk, Image
  8.  
  9. window = Tk()
  10. window.geometry('800x800+200+300')
  11.  
  12.  
  13. class ImageEditor:
  14.     path = ''
  15.  
  16.     def create_image(self):
  17.         self.img = cv2.imread(self.path)
  18.         self.img = cv2.cvtColor(self.img, cv2.COLOR_BGR2RGB)
  19.  
  20.  
  21. image_editor = ImageEditor()
  22.  
  23.  
  24. def open_img():
  25.     file = filedialog.askopenfilename()
  26.     image_editor.path = file
  27.     image_editor.create_image()
  28.  
  29.     img = Image.open(file)
  30.     image = ImageTk.PhotoImage(img.resize((700, 500), Image.Resampling.NEAREST))
  31.  
  32.     label_image = Label(window, image=image)
  33.     label_image.image = image
  34.     label_image.place(x=0, y=300, anchor="w")
  35.  
  36.  
  37. def cartoon_func():
  38.     gray = cv2.cvtColor(image_editor.img, cv2.COLOR_BGR2GRAY)
  39.     gray = cv2.medianBlur(gray, 5)
  40.     edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 9)
  41.     color = cv2.bilateralFilter(image_editor.img, 9, 250, 250)
  42.     cartoon = cv2.bitwise_and(color, color, mask=edges)
  43.     plt.figure(figsize=(10, 10))
  44.     plt.imshow(cartoon, cmap="gray")
  45.     plt.axis("off")
  46.     plt.title("Cartoon Image")
  47.     plt.show()
  48.  
  49.     color = cv2.resize(cartoon, (700, 500), interpolation=cv2.INTER_AREA)
  50.     image = cv2.cvtColor(color, cv2.COLOR_BGR2RGB)
  51.     image = ImageTk.PhotoImage(image=PIL.Image.fromarray(image))
  52.  
  53.     label_image = Label(window, image=image)
  54.     label_image.image = image
  55.     label_image.place(x=0, y=300, anchor="w")
  56.  
  57.  
  58. def black_white_func():
  59.     gray = cv2.cvtColor(image_editor.img, cv2.COLOR_BGR2GRAY)
  60.     gray = cv2.medianBlur(gray, 5)
  61.     plt.figure(figsize=(10, 10))
  62.     plt.imshow(gray, cmap="gray")
  63.     plt.axis("off")
  64.     plt.title("Grayscale Image")
  65.     plt.show()
  66.     gray = cv2.resize(gray, (700, 500), interpolation=cv2.INTER_AREA)
  67.     image = cv2.cvtColor(gray, cv2.COLOR_BGR2RGB)
  68.  
  69.     image = ImageTk.PhotoImage(image=PIL.Image.fromarray(image))
  70.  
  71.     label_image = Label(window, image=image)
  72.     label_image.image = image
  73.     label_image.place(x=0, y=300, anchor="w")
  74.  
  75.  
  76. button_open = Button(window, text='Открыть картинку', command=open_img)
  77. button_open.place(x=0, y=0)
  78.  
  79. button_bw = Button(window, text='Сделать картинку в чб', command=black_white_func)
  80. button_bw.place(x=200, y=0)
  81.  
  82. button_cartoon = Button(window, text='Сделать картинку в мультяшный стиль', command=cartoon_func)
  83. button_cartoon.place(x=400, y=0)
  84.  
  85. window.mainloop()
  86.  
Advertisement
Add Comment
Please, Sign In to add comment