Advertisement
azizkhelifi

Untitled

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