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)
- im_size = photo.size
- title.set(f"Dimension de l'image: {im_size[0]}x{im_size[1]}")
- 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):
- L=get_color_histogram(image)
- histogram_image = Image.new("RGB", IMAGE_SIZE, "white")
- draw = ImageDraw.Draw(histogram_image)
- draw.line(list(enumerate(L[0])), fill='red', width=1)
- draw.line(list(enumerate(L[1])), fill='green', width=1)
- draw.line(list(enumerate(L[2])), fill='blue', width=1)
- histogram_photo = ImageTk.PhotoImage(histogram_image)
- canva.create_image(0, 0, image=histogram_photo, anchor="nw")
- canva.photo = histogram_photo
- # 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
- def get_color_histogram(image):
- image = image.convert('RGB')
- pixels = image.load()
- red_histogram = [0] * 256
- green_histogram = [0] * 256
- blue_histogram = [0] * 256
- for i in range(image.width):
- for j in range(image.height):
- r, g, b = pixels[i, j]
- red_histogram[r] += 1
- green_histogram[g] += 1
- blue_histogram[b] += 1
- return red_histogram, green_histogram, blue_histogram
- window = Tk()
- window.geometry("850x750")
- window.title("My interface")
- window.config(bg="white")
- title_frame = Frame(window, width=200, height=100, bg="white", pady=20)
- title_frame.pack(side="top", fill='x')
- 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)
- # add label with large size
- title = StringVar()
- title.set("Upload image")
- title_label = Label(title_frame, textvariable=title, bg="white", font=("Arial", 25), width=4000)
- title_label.pack()
- 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