Advertisement
Najeebsk

IMAGES-SEARCH.py

Mar 3rd, 2024
866
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. import os
  2. from tkinter import Tk, Listbox, Scrollbar, Button, Label
  3. from PIL import Image, ImageTk
  4.  
  5. def search_images(folder):
  6.     image_files = []
  7.     for root, dirs, files in os.walk(folder):
  8.         for file in files:
  9.             if file.endswith((".jpg", ".png", ".bmp")):
  10.                 image_files.append(os.path.join(root, file))
  11.     return image_files
  12.  
  13. def show_image():
  14.     selected_index = listbox.curselection()
  15.     if selected_index:
  16.         selected_image_path = listbox.get(selected_index)
  17.         image = Image.open(selected_image_path)
  18.         image.thumbnail((400, 400))  # Resize the image to fit the window
  19.         photo = ImageTk.PhotoImage(image)
  20.        
  21.         # Update the label with the new image
  22.         image_label.config(image=photo)
  23.         image_label.image = photo  # Keep a reference to the image to prevent garbage collection
  24.  
  25. # Create GUI window
  26. root = Tk()
  27. root.title("Image Viewer")
  28.  
  29. # Create listbox and scrollbar
  30. listbox = Listbox(root, width=50, height=20)
  31. scrollbar = Scrollbar(root, orient="vertical", command=listbox.yview)
  32. scrollbar.pack(side="right", fill="y")
  33. listbox.config(yscrollcommand=scrollbar.set)
  34.  
  35. # Search for images in all drives and subfolders
  36. drives = [chr(i) for i in range(65, 91) if os.path.exists(chr(i) + ":\\")]
  37. for drive in drives:
  38.     images = search_images(drive + ":\\")
  39.     for image in images:
  40.         listbox.insert("end", image)
  41.  
  42. listbox.pack(side="left", fill="both", expand=True)
  43.  
  44. # Create button to view selected image
  45. view_button = Button(root, text="View Image", command=show_image)
  46. view_button.pack()
  47.  
  48. # Create label to display image
  49. image_label = Label(root)
  50. image_label.pack()
  51.  
  52. root.mainloop()
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement