Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from tkinter import Tk, Listbox, Scrollbar, Button, Label
- from PIL import Image, ImageTk
- def search_images(folder):
- image_files = []
- for root, dirs, files in os.walk(folder):
- for file in files:
- if file.endswith((".jpg", ".png", ".bmp")):
- image_files.append(os.path.join(root, file))
- return image_files
- def show_image():
- selected_index = listbox.curselection()
- if selected_index:
- selected_image_path = listbox.get(selected_index)
- image = Image.open(selected_image_path)
- image.thumbnail((400, 400)) # Resize the image to fit the window
- photo = ImageTk.PhotoImage(image)
- # Update the label with the new image
- image_label.config(image=photo)
- image_label.image = photo # Keep a reference to the image to prevent garbage collection
- # Create GUI window
- root = Tk()
- root.title("Image Viewer")
- # Create listbox and scrollbar
- listbox = Listbox(root, width=50, height=20)
- scrollbar = Scrollbar(root, orient="vertical", command=listbox.yview)
- scrollbar.pack(side="right", fill="y")
- listbox.config(yscrollcommand=scrollbar.set)
- # Search for images in all drives and subfolders
- drives = [chr(i) for i in range(65, 91) if os.path.exists(chr(i) + ":\\")]
- for drive in drives:
- images = search_images(drive + ":\\")
- for image in images:
- listbox.insert("end", image)
- listbox.pack(side="left", fill="both", expand=True)
- # Create button to view selected image
- view_button = Button(root, text="View Image", command=show_image)
- view_button.pack()
- # Create label to display image
- image_label = Label(root)
- image_label.pack()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement