Advertisement
azizkhelifi

Untitled

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