Advertisement
azizkhelifi

Untitled

Mar 2nd, 2023
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.90 KB | None | 0 0
  1. from tkinter import *
  2. from tkinter import filedialog
  3. from PIL import Image, ImageTk,ImageDraw
  4. from tkinter import ttk
  5.  
  6. window = Tk()
  7. window.geometry("1000x600")
  8. window.title("My interface")
  9. window.config(bg="white")
  10.  
  11. photo = None
  12.  
  13. def upload():
  14.     global photo
  15.     chemin = filedialog.askopenfilename()
  16.     photo = Image.open(chemin)
  17.     # width, height = int(photo.width / 3), int(photo.height / 3)
  18.     photo = photo.resize((500,500))
  19.     photo_tk = ImageTk.PhotoImage(photo)
  20.     image_canva.create_image(0, 0, image=photo_tk, anchor="nw")
  21.     image_canva.photo = photo_tk
  22.     show_histogram(photo)
  23.  
  24. def apply_filter(filter):
  25.     global photo
  26.     _photo = photo
  27.     if filter == "Noir & Blanc":
  28.         _photo = _photo.convert('L')
  29.         photo_tk = ImageTk.PhotoImage(_photo)
  30.         image_canva.create_image(0, 0, image=photo_tk, anchor="nw")
  31.         image_canva.photo = photo_tk
  32.         show_histogram(_photo)
  33.  
  34.     elif filter == "Contour":
  35.         _photo = _photo.convert("1")
  36.         photo_tk = ImageTk.PhotoImage(_photo)
  37.         image_canva.create_image(0, 0, image=photo_tk, anchor="nw")
  38.         image_canva.photo = photo_tk
  39.         show_histogram(_photo)
  40.     # elif filter == "Histogramme":
  41.     #     show_histogram(photo)
  42.     elif filter == "Original":
  43.         photo_tk = ImageTk.PhotoImage(photo)
  44.         image_canva.create_image(0, 0, image=photo_tk, anchor="nw")
  45.         image_canva.photo = photo_tk
  46.         show_histogram(photo)
  47.  
  48. def show_histogram(photo):
  49.     # Calculate the histogram
  50.     histogram = photo.histogram()
  51.  
  52.     # Create a new image for the histogram
  53.     histogram_image = Image.new("RGB", (256, 200), "white")
  54.     histogram_draw = ImageDraw.Draw(histogram_image)
  55.  
  56.     # Find the maximum value in the histogram to scale the bars
  57.     max_value = max(histogram)
  58.  
  59.     # Draw the bars of the histogram
  60.     for i in range(256):
  61.         bar_height = int(histogram[i] / max_value * 200)
  62.         histogram_draw.line((i, 200, i, 200 - bar_height), fill=(i, i, i))
  63.  
  64.     # Convert the histogram image to a Tkinter PhotoImage and display it
  65.     histogram_photo = ImageTk.PhotoImage(histogram_image)
  66.     hist.create_image(0, 0, image=histogram_photo, anchor="nw")
  67.     hist.photo = histogram_photo
  68. # def show_histogram():
  69. #     global photo
  70. #     # hist.delete("all")
  71. #     histogram = photo.histogram()
  72. #     # print(histo)
  73. #     max_count = max(histogram)
  74. #     width = 250
  75. #     height = 200
  76. #     bar_width = width // len(histogram)
  77. #     for i in range(len(histogram)):
  78. #         bar_height = histogram[i] * height // max_count
  79. #         hist.create_rectangle(i*bar_width, height-bar_height, (i+1)*bar_width, height, fill="blue")
  80.  
  81. sep = Frame(window, width=200, height=600, bg="white")
  82. sep.pack(side="left", fill='y')
  83.  
  84. right = Frame(window, width=300, height=600, bg="white")
  85. right.pack(side="right", fill='y')
  86.  
  87. Load = Button(sep, text="Load image", command=upload,  bg="light blue")
  88. Load.pack(pady=100)
  89.  
  90. Convert_nb = Button(sep, text="Original", command=lambda: apply_filter("Original"), bg="light blue")
  91. Convert_nb.pack(pady=10)
  92.  
  93. Convert_nb = Button(sep, text="Convert N/B", command=lambda: apply_filter("Noir & Blanc"), bg="light blue")
  94. Convert_nb.pack(pady=10)
  95.  
  96. Contour = Button(sep, text="Contour", command=lambda: apply_filter("Contour"), bg="light blue")
  97. Contour.pack(pady=10)
  98.  
  99. histo = Button(sep, text="Histogramme", command=lambda: apply_filter("Histogramme"), bg="light blue")
  100. histo.pack(pady=10)
  101.  
  102. image_canva = Canvas(window, width=500, height=500, bg="white")
  103. image_canva.pack(pady=20)
  104.  
  105. hist = Canvas(right, width=500, height=500, bg="white")
  106. hist.pack(pady=20)
  107. # NB = Canvas(window, width=200, height=150, bg="white")
  108. # NB.pack(pady=20)
  109.  
  110. # Sketch = Canvas(window, width=200, height=150, bg="white")
  111. # Sketch.pack(pady=20)
  112.  
  113. # filtres = ttk.Combobox(sep, values=("Noir & Blanc", "Contour", "Histogramme"))
  114. # filtres.pack(pady=10)
  115.  
  116. window.mainloop()
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement