Advertisement
azizkhelifi

Untitled

Mar 2nd, 2023
885
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.03 KB | None | 0 0
  1. from tkinter import *
  2. from tkinter import filedialog
  3. from PIL import Image, ImageTk, ImageDraw
  4.  
  5. IMAGE_SIZE = (350, 350)
  6.  
  7. photo = None
  8.  
  9.  
  10. def upload():
  11.     global photo
  12.     path = filedialog.askopenfilename()
  13.     if path:
  14.         photo = Image.open(path)
  15.         photo = photo.resize(IMAGE_SIZE)
  16.         photo_tk = ImageTk.PhotoImage(photo)
  17.         image_canvases[0].create_image(0, 0, image=photo_tk, anchor="nw")
  18.         image_canvases[0].photo = photo_tk
  19.         show_histogram(photo, original_image_histo)
  20.         im_size = photo.size
  21.         title.set(f"Dimension de l'image: {im_size[0]}x{im_size[1]}")
  22.  
  23.  
  24. def apply_filter(filter_name):
  25.     global photo
  26.  
  27.     if filter_name == "Noir & Blanc":
  28.         filtered_photo = photo.convert('L')
  29.         canva = noir_blanc_canva
  30.     elif filter_name == "Contour":
  31.         filtered_photo = photo.convert("1")
  32.         canva = contour_canva
  33.     photo_tk = ImageTk.PhotoImage(filtered_photo)
  34.     canva.create_image(0, 0, image=photo_tk, anchor="nw")
  35.     canva.photo = photo_tk
  36.  
  37.  
  38. def show_histogram(image, canva):
  39.     L=get_color_histogram(image)
  40.     histogram_image = Image.new("RGB", IMAGE_SIZE, "white")
  41.     draw = ImageDraw.Draw(histogram_image)
  42.     draw.line(list(enumerate(L[0])), fill='red', width=1)
  43.     draw.line(list(enumerate(L[1])), fill='green', width=1)
  44.     draw.line(list(enumerate(L[2])), fill='blue', width=1)
  45.     histogram_photo = ImageTk.PhotoImage(histogram_image)
  46.     canva.create_image(0, 0, image=histogram_photo, anchor="nw")
  47.     canva.photo = histogram_photo
  48.     # global histogram_photo
  49.     # histogram = image.histogram()
  50.  
  51.     # histogram_image = Image.new("RGB", IMAGE_SIZE, "white")
  52.     # histogram_draw = ImageDraw.Draw(histogram_image)
  53.  
  54.     # max_value = max(histogram)
  55.  
  56.     # for i in range(256):
  57.     #     bar_height = int(histogram[i] / max_value * IMAGE_SIZE[1])
  58.     #     histogram_draw.line((i, IMAGE_SIZE[1], i, IMAGE_SIZE[0] - bar_height), fill=(i, i, i))
  59.     # histogram_photo = ImageTk.PhotoImage(histogram_image)
  60.     # canva.create_image(0, 0, image=histogram_photo, anchor="nw")
  61.     # canva.photo = histogram_photo
  62.  
  63. def get_color_histogram(image):
  64.     image = image.convert('RGB')
  65.     pixels = image.load()
  66.     red_histogram = [0] * 256
  67.     green_histogram = [0] * 256
  68.     blue_histogram = [0] * 256
  69.  
  70.     for i in range(image.width):
  71.         for j in range(image.height):
  72.             r, g, b = pixels[i, j]
  73.             red_histogram[r] += 1
  74.             green_histogram[g] += 1
  75.             blue_histogram[b] += 1
  76.  
  77.     return red_histogram, green_histogram, blue_histogram
  78. window = Tk()
  79. window.geometry("850x750")
  80. window.title("My interface")
  81. window.config(bg="white")
  82.  
  83. title_frame = Frame(window, width=200, height=100, bg="white", pady=20)
  84. title_frame.pack(side="top", fill='x')
  85.  
  86. buttons_frame = Frame(window, width=200, height=600, bg="white", pady=200)
  87. buttons_frame.pack(side="left", fill='y')
  88.  
  89. load_button = Button(buttons_frame, text="Load image", command=upload, bg="light blue")
  90. load_button.pack(side='top', pady=10)
  91.  
  92.  
  93.  
  94. nb_button = Button(buttons_frame, text="Convert N/B", command=lambda: apply_filter("Noir & Blanc"), bg="light blue")
  95. nb_button.pack(side='top', pady=10)
  96.  
  97. contour_button = Button(buttons_frame, text="Contour", command=lambda: apply_filter("Contour"), bg="light blue")
  98. contour_button.pack(side='top', pady=10)
  99.  
  100. images_frame = Frame(window, width=800, height=600, bg="white")
  101. images_frame.pack(side='right', fill='both', expand=True)
  102.  
  103. # add label with large size
  104.  
  105. title = StringVar()
  106. title.set("Upload image")
  107. title_label = Label(title_frame, textvariable=title, bg="white",  font=("Arial", 25), width=4000)
  108. title_label.pack()
  109.  
  110.  
  111. image_canvases = []
  112. for i in range(4):
  113.     canvas = Canvas(images_frame, width=IMAGE_SIZE[0], height=IMAGE_SIZE[1], bg="white")
  114.     canvas.grid(row=i//2, column=i%2)
  115.     image_canvases.append(canvas)
  116.  
  117. original_image_canvas = image_canvases[0]
  118. original_image_histo = image_canvases[1]
  119. noir_blanc_canva = image_canvases[2]
  120. contour_canva = image_canvases[3]
  121. window.mainloop()
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement