Advertisement
azizkhelifi

Untitled

Mar 2nd, 2023
946
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.62 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.  
  21.  
  22. def apply_filter(filter_name):
  23.     global photo
  24.  
  25.     if filter_name == "Noir & Blanc":
  26.         filtered_photo = photo.convert('L')
  27.         canva = noir_blanc_canva
  28.     elif filter_name == "Contour":
  29.         filtered_photo = photo.convert("1")
  30.         canva = contour_canva
  31.     photo_tk = ImageTk.PhotoImage(filtered_photo)
  32.     canva.create_image(0, 0, image=photo_tk, anchor="nw")
  33.     canva.photo = photo_tk
  34.  
  35.  
  36. def show_histogram(image, canva):
  37.     L=get_color_histogram(image)
  38.     histogram_image = Image.new("RGB", IMAGE_SIZE, "white")
  39.     draw = ImageDraw.Draw(histogram_image)
  40.     draw.line(list(enumerate(L[0])), fill='red', width=1)
  41.     draw.line(list(enumerate(L[1])), fill='green', width=1)
  42.     draw.line(list(enumerate(L[2])), fill='blue', width=1)
  43.     histogram_photo = ImageTk.PhotoImage(histogram_image)
  44.     canva.create_image(0, 0, image=histogram_photo, anchor="nw")
  45.     canva.photo = histogram_photo
  46.     # global histogram_photo
  47.     # histogram = image.histogram()
  48.  
  49.     # histogram_image = Image.new("RGB", IMAGE_SIZE, "white")
  50.     # histogram_draw = ImageDraw.Draw(histogram_image)
  51.  
  52.     # max_value = max(histogram)
  53.  
  54.     # for i in range(256):
  55.     #     bar_height = int(histogram[i] / max_value * IMAGE_SIZE[1])
  56.     #     histogram_draw.line((i, IMAGE_SIZE[1], i, IMAGE_SIZE[0] - bar_height), fill=(i, i, i))
  57.     # histogram_photo = ImageTk.PhotoImage(histogram_image)
  58.     # canva.create_image(0, 0, image=histogram_photo, anchor="nw")
  59.     # canva.photo = histogram_photo
  60.  
  61. def get_color_histogram(image):
  62.     image = image.convert('RGB')
  63.     pixels = image.load()
  64.     red_histogram = [0] * 256
  65.     green_histogram = [0] * 256
  66.     blue_histogram = [0] * 256
  67.  
  68.     for i in range(image.width):
  69.         for j in range(image.height):
  70.             r, g, b = pixels[i, j]
  71.             red_histogram[r] += 1
  72.             green_histogram[g] += 1
  73.             blue_histogram[b] += 1
  74.  
  75.     return red_histogram, green_histogram, blue_histogram
  76. window = Tk()
  77. window.geometry("850x750")
  78. window.title("My interface")
  79. window.config(bg="white")
  80.  
  81. buttons_frame = Frame(window, width=200, height=600, bg="white", pady=200)
  82. buttons_frame.pack(side="left", fill='y')
  83.  
  84. load_button = Button(buttons_frame, text="Load image", command=upload, bg="light blue")
  85. load_button.pack(side='top', pady=10)
  86.  
  87.  
  88. nb_button = Button(buttons_frame, text="Convert N/B", command=lambda: apply_filter("Noir & Blanc"), bg="light blue")
  89. nb_button.pack(side='top', pady=10)
  90.  
  91. contour_button = Button(buttons_frame, text="Contour", command=lambda: apply_filter("Contour"), bg="light blue")
  92. contour_button.pack(side='top', pady=10)
  93.  
  94. images_frame = Frame(window, width=800, height=600, bg="white")
  95. images_frame.pack(side='right', fill='both', expand=True)
  96.  
  97. image_canvases = []
  98. for i in range(4):
  99.     canvas = Canvas(images_frame, width=IMAGE_SIZE[0], height=IMAGE_SIZE[1], bg="white")
  100.     canvas.grid(row=i//2, column=i%2)
  101.     image_canvases.append(canvas)
  102.  
  103. original_image_canvas = image_canvases[0]
  104. original_image_histo = image_canvases[1]
  105. noir_blanc_canva = image_canvases[2]
  106. contour_canva = image_canvases[3]
  107. window.mainloop()
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement