What's new

Help Image Viewer in Python?

SHOCKDART

Leecher
Joined
Mar 10, 2023
Posts
1
Reaction
0
Points
2
Warning: Avoid posting in all capital letters
IMAGE VIEWER

LET USER CHOOSE (OPTION) A DIRECTORY / FOLDER

CAN READ ALL IMAGE FILE IN THAT FOLDER (PNG, JPG, etc)

CAN SHOW IMAGE IN GALLERY / SLIDESHOW VIEW (CYCLE)

ABLE TO SWITCH BETWEEN VIEWS

SINGLE-WINDOW APP
 
To create an image viewer in Python that meets the requirements you listed, you can use the following steps:

1. Import the necessary modules:
Code:
import os
import glob
from tkinter import *
from PIL import Image, ImageTk

2. Create a function that allows the user to choose a directory/folder:
Code:
def choose_directory():
    global filepath
    filepath = filedialog.askdirectory()

3. Create a function that reads all the image files in the chosen directory:
Code:
def read_images():
    global images
    images = []
    for file in glob.glob(filepath + "/*.png") + glob.glob(filepath + "/*.jpg"):
        image = Image.open(file)
        images.append(image)

4. Create a function that displays the images in a gallery/slideshow view:
Code:
def display_images():
    global current_image
    current_image = 0
    global image_label
    image_label = Label(image_display_frame)
    image_label.pack()
    show_image()
    root.after(2000, display_images)
    
def show_image():
    global current_image
    global images
    if current_image == len(images):
        current_image = 0
    image = images[current_image].resize((800, 600))
    photo = ImageTk.PhotoImage(image)
    image_label.configure(image=photo)
    image_label.image = photo
    current_image += 1

5. Create a function that allows the user to switch between the gallery/slideshow view and the file explorer view:
Code:
def switch_view():
    global current_view
    if current_view == "gallery":
        image_display_frame.pack_forget()
        file_display_frame.pack()
        current_view = "file explorer"
        switch_button.configure(text="Switch to Gallery View")
    else:
        file_display_frame.pack_forget()
        image_display_frame.pack()
        current_view = "gallery"
        switch_button.configure(text="Switch to File Explorer View")

6. Create the GUI:
Code:
root = Tk()
root.title("Image Viewer")
root.geometry("800x600")
filepath = ""
images = []
current_image = 0
current_view = "file explorer"

# File Explorer View
file_display_frame = Frame(root)
file_display_frame.pack()

choose_button = Button(file_display_frame, text="Choose Directory", command=choose_directory)
choose_button.pack(pady=10)

read_button = Button(file_display_frame, text="Read Images", command=read_images)
read_button.pack(pady=10)

switch_button = Button(file_display_frame, text="Switch to Gallery View", command=switch_view)
switch_button.pack(pady=10)

# Gallery/Slideshow View
image_display_frame = Frame(root)
display_images()

root.mainloop()

This will create a single-window image viewer application that allows the user to choose a directory/folder, read all the image files in that folder, and display the images in a gallery/slideshow view. The user can switch between the gallery/slideshow view and the file explorer view by clicking a button.
 
Back
Top