Advertisement
CodeCrusader

Image Gallery GUI App

Jun 10th, 2023
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.57 KB | Software | 0 0
  1. import tkinter as tk
  2. from PIL import ImageTk, Image
  3. import os
  4.  
  5. # Initialize the window
  6. window = tk.Tk()
  7. window.title("Image Gallery")
  8.  
  9. # Directory containing the images
  10. image_directory = "path_to_image_directory"
  11.  
  12. # Get the list of image files in the directory
  13. image_files = [file for file in os.listdir(image_directory) if file.endswith((".jpg", ".jpeg", ".png"))]
  14.  
  15. # Initialize variables
  16. current_image = 0
  17.  
  18. # Function to display the current image
  19. def display_image():
  20.     global current_image
  21.  
  22.     # Load the current image file
  23.     image_path = os.path.join(image_directory, image_files[current_image])
  24.     image = Image.open(image_path)
  25.     image.thumbnail((500, 500))
  26.     photo = ImageTk.PhotoImage(image)
  27.  
  28.     # Update the image label
  29.     image_label.configure(image=photo)
  30.     image_label.image = photo
  31.  
  32. # Function to move to the previous image
  33. def prev_image():
  34.     global current_image
  35.     if current_image > 0:
  36.         current_image -= 1
  37.         display_image()
  38.  
  39. # Function to move to the next image
  40. def next_image():
  41.     global current_image
  42.     if current_image < len(image_files) - 1:
  43.         current_image += 1
  44.         display_image()
  45.  
  46. # Create a label to display the image
  47. image_label = tk.Label(window)
  48. image_label.pack()
  49.  
  50. # Create buttons for navigation
  51. prev_button = tk.Button(window, text="Prev", command=prev_image)
  52. prev_button.pack(side=tk.LEFT, padx=10)
  53.  
  54. next_button = tk.Button(window, text="Next", command=next_image)
  55. next_button.pack(side=tk.RIGHT, padx=10)
  56.  
  57. # Display the initial image
  58. display_image()
  59.  
  60. window.mainloop()
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement