Guest User

Python - Tkinter browse image files and show

a guest
Oct 22nd, 2023
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.66 KB | Source Code | 0 0
  1. import tkinter as tk
  2. from tkinter import filedialog
  3. from PIL import Image, ImageTk  # pip install pillow
  4.  
  5. root = tk.Tk()
  6. root.title("Image Viewer")
  7.  
  8. image_label = tk.Label(root)
  9. image_label.pack()
  10.  
  11.  
  12. def open_image():
  13.     file_path = filedialog.askopenfilename(
  14.         filetypes=[
  15.             ("Image files", "*.png *.jpg *.jpeg *.gif *.bmp *.ppm *.pgm")
  16.         ]
  17.     )
  18.     if file_path:
  19.         image = Image.open(file_path)
  20.         photo = ImageTk.PhotoImage(image)
  21.         image_label.config(image=photo)
  22.         image_label.image = photo
  23.  
  24.  
  25. open_button = tk.Button(root, text="Open Image", command=open_image)
  26. open_button.pack()
  27.  
  28. root.mainloop()
  29.  
Advertisement
Add Comment
Please, Sign In to add comment