Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from tkinter import filedialog
- from PIL import Image, ImageTk, ImageDraw
- IMAGE_SIZE = (350, 350)
- photo = None
- def upload():
- global photo
- path = filedialog.askopenfilename()
- if path:
- photo = Image.open(path)
- photo = photo.resize(IMAGE_SIZE)
- photo_tk = ImageTk.PhotoImage(photo)
- image_canvases[0].create_image(0, 0, image=photo_tk, anchor="nw")
- image_canvases[0].photo = photo_tk
- show_histogram(photo, original_image_histo)
- def apply_filter(filter_name):
- global photo
- if filter_name == "Noir & Blanc":
- filtered_photo = photo.convert('L')
- canva = noir_blanc_canva
- elif filter_name == "Contour":
- filtered_photo = photo.convert("1")
- canva = contour_canva
- photo_tk = ImageTk.PhotoImage(filtered_photo)
- canva.create_image(0, 0, image=photo_tk, anchor="nw")
- canva.photo = photo_tk
- def show_histogram(image, canva):
- # global histogram_photo
- histogram = image.histogram()
- histogram_image = Image.new("RGB", IMAGE_SIZE, "white")
- histogram_draw = ImageDraw.Draw(histogram_image)
- max_value = max(histogram)
- for i in range(256):
- bar_height = int(histogram[i] / max_value * IMAGE_SIZE[1])
- histogram_draw.line((i, IMAGE_SIZE[1], i, IMAGE_SIZE[0] - bar_height), fill=(i, i, i))
- histogram_photo = ImageTk.PhotoImage(histogram_image)
- canva.create_image(0, 0, image=histogram_photo, anchor="nw")
- canva.photo = histogram_photo
- window = Tk()
- window.geometry("850x750")
- window.title("My interface")
- window.config(bg="white")
- buttons_frame = Frame(window, width=200, height=600, bg="white", pady=200)
- buttons_frame.pack(side="left", fill='y')
- load_button = Button(buttons_frame, text="Load image", command=upload, bg="light blue")
- load_button.pack(side='top', pady=10)
- nb_button = Button(buttons_frame, text="Convert N/B", command=lambda: apply_filter("Noir & Blanc"), bg="light blue")
- nb_button.pack(side='top', pady=10)
- contour_button = Button(buttons_frame, text="Contour", command=lambda: apply_filter("Contour"), bg="light blue")
- contour_button.pack(side='top', pady=10)
- images_frame = Frame(window, width=800, height=600, bg="white")
- images_frame.pack(side='right', fill='both', expand=True)
- image_canvases = []
- for i in range(4):
- canvas = Canvas(images_frame, width=IMAGE_SIZE[0], height=IMAGE_SIZE[1], bg="white")
- canvas.grid(row=i//2, column=i%2)
- image_canvases.append(canvas)
- original_image_canvas = image_canvases[0]
- original_image_histo = image_canvases[1]
- noir_blanc_canva = image_canvases[2]
- contour_canva = image_canvases[3]
- window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement