Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from tkinter import filedialog
- from PIL import Image, ImageTk,ImageDraw
- from tkinter import ttk
- window = Tk()
- window.geometry("1000x600")
- window.title("My interface")
- window.config(bg="white")
- photo = None
- def upload():
- global photo
- chemin = filedialog.askopenfilename()
- photo = Image.open(chemin)
- # width, height = int(photo.width / 3), int(photo.height / 3)
- photo = photo.resize((500,500))
- photo_tk = ImageTk.PhotoImage(photo)
- image_canva.create_image(0, 0, image=photo_tk, anchor="nw")
- image_canva.photo = photo_tk
- show_histogram(photo)
- def apply_filter(filter):
- global photo
- _photo = photo
- if filter == "Noir & Blanc":
- _photo = _photo.convert('L')
- photo_tk = ImageTk.PhotoImage(_photo)
- image_canva.create_image(0, 0, image=photo_tk, anchor="nw")
- image_canva.photo = photo_tk
- show_histogram(_photo)
- elif filter == "Contour":
- _photo = _photo.convert("1")
- photo_tk = ImageTk.PhotoImage(_photo)
- image_canva.create_image(0, 0, image=photo_tk, anchor="nw")
- image_canva.photo = photo_tk
- show_histogram(_photo)
- # elif filter == "Histogramme":
- # show_histogram(photo)
- elif filter == "Original":
- photo_tk = ImageTk.PhotoImage(photo)
- image_canva.create_image(0, 0, image=photo_tk, anchor="nw")
- image_canva.photo = photo_tk
- show_histogram(photo)
- def show_histogram(photo):
- # Calculate the histogram
- histogram = photo.histogram()
- # Create a new image for the histogram
- histogram_image = Image.new("RGB", (256, 200), "white")
- histogram_draw = ImageDraw.Draw(histogram_image)
- # Find the maximum value in the histogram to scale the bars
- max_value = max(histogram)
- # Draw the bars of the histogram
- for i in range(256):
- bar_height = int(histogram[i] / max_value * 200)
- histogram_draw.line((i, 200, i, 200 - bar_height), fill=(i, i, i))
- # Convert the histogram image to a Tkinter PhotoImage and display it
- histogram_photo = ImageTk.PhotoImage(histogram_image)
- hist.create_image(0, 0, image=histogram_photo, anchor="nw")
- hist.photo = histogram_photo
- # def show_histogram():
- # global photo
- # # hist.delete("all")
- # histogram = photo.histogram()
- # # print(histo)
- # max_count = max(histogram)
- # width = 250
- # height = 200
- # bar_width = width // len(histogram)
- # for i in range(len(histogram)):
- # bar_height = histogram[i] * height // max_count
- # hist.create_rectangle(i*bar_width, height-bar_height, (i+1)*bar_width, height, fill="blue")
- sep = Frame(window, width=200, height=600, bg="white")
- sep.pack(side="left", fill='y')
- right = Frame(window, width=300, height=600, bg="white")
- right.pack(side="right", fill='y')
- Load = Button(sep, text="Load image", command=upload, bg="light blue")
- Load.pack(pady=100)
- Convert_nb = Button(sep, text="Original", command=lambda: apply_filter("Original"), bg="light blue")
- Convert_nb.pack(pady=10)
- Convert_nb = Button(sep, text="Convert N/B", command=lambda: apply_filter("Noir & Blanc"), bg="light blue")
- Convert_nb.pack(pady=10)
- Contour = Button(sep, text="Contour", command=lambda: apply_filter("Contour"), bg="light blue")
- Contour.pack(pady=10)
- histo = Button(sep, text="Histogramme", command=lambda: apply_filter("Histogramme"), bg="light blue")
- histo.pack(pady=10)
- image_canva = Canvas(window, width=500, height=500, bg="white")
- image_canva.pack(pady=20)
- hist = Canvas(right, width=500, height=500, bg="white")
- hist.pack(pady=20)
- # NB = Canvas(window, width=200, height=150, bg="white")
- # NB.pack(pady=20)
- # Sketch = Canvas(window, width=200, height=150, bg="white")
- # Sketch.pack(pady=20)
- # filtres = ttk.Combobox(sep, values=("Noir & Blanc", "Contour", "Histogramme"))
- # filtres.pack(pady=10)
- window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement