Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from tkinter import filedialog
- import PIL
- import matplotlib.pyplot as plt
- import cv2
- from PIL import ImageTk, Image
- window = Tk()
- window.geometry('800x800+200+300')
- class ImageEditor:
- path = ''
- def create_image(self):
- self.img = cv2.imread(self.path)
- self.img = cv2.cvtColor(self.img, cv2.COLOR_BGR2RGB)
- image_editor = ImageEditor()
- def open_img():
- file = filedialog.askopenfilename()
- image_editor.path = file
- image_editor.create_image()
- img = Image.open(file)
- image = ImageTk.PhotoImage(img.resize((700, 500), Image.Resampling.NEAREST))
- label_image = Label(window, image=image)
- label_image.image = image
- label_image.place(x=0, y=300, anchor="w")
- def cartoon_func():
- gray = cv2.cvtColor(image_editor.img, cv2.COLOR_BGR2GRAY)
- gray = cv2.medianBlur(gray, 5)
- edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 9)
- color = cv2.bilateralFilter(image_editor.img, 9, 250, 250)
- cartoon = cv2.bitwise_and(color, color, mask=edges)
- plt.figure(figsize=(10, 10))
- plt.imshow(cartoon, cmap="gray")
- plt.axis("off")
- plt.title("Cartoon Image")
- plt.show()
- color = cv2.resize(cartoon, (700, 500), interpolation=cv2.INTER_AREA)
- image = cv2.cvtColor(color, cv2.COLOR_BGR2RGB)
- image = ImageTk.PhotoImage(image=PIL.Image.fromarray(image))
- label_image = Label(window, image=image)
- label_image.image = image
- label_image.place(x=0, y=300, anchor="w")
- def black_white_func():
- gray = cv2.cvtColor(image_editor.img, cv2.COLOR_BGR2GRAY)
- gray = cv2.medianBlur(gray, 5)
- plt.figure(figsize=(10, 10))
- plt.imshow(gray, cmap="gray")
- plt.axis("off")
- plt.title("Grayscale Image")
- plt.show()
- gray = cv2.resize(gray, (700, 500), interpolation=cv2.INTER_AREA)
- image = cv2.cvtColor(gray, cv2.COLOR_BGR2RGB)
- image = ImageTk.PhotoImage(image=PIL.Image.fromarray(image))
- label_image = Label(window, image=image)
- label_image.image = image
- label_image.place(x=0, y=300, anchor="w")
- button_open = Button(window, text='Открыть картинку', command=open_img)
- button_open.place(x=0, y=0)
- button_bw = Button(window, text='Сделать картинку в чб', command=black_white_func)
- button_bw.place(x=200, y=0)
- button_cartoon = Button(window, text='Сделать картинку в мультяшный стиль', command=cartoon_func)
- button_cartoon.place(x=400, y=0)
- window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment