Image Viewer: Python App to View and Navigate Images

 

Introduction

In this guide, we will walk you through creating a simple image viewer app using Python. This app will allow users to view and navigate through images on their local machine. Whether you’re managing a collection of photos or simply want to view images with ease, this app will provide a simple and efficient solution for image viewing.

Objective

The objective of this project is to develop a Python-based Image Viewer application that allows users to open, view, and navigate through images (forward and backward) with an easy-to-use interface. By using the popular Tkinter library for GUI development, we will create a program that is intuitive and simple to use for anyone.

Code


import os
from tkinter import *
from tkinter import filedialog
from PIL import Image, ImageTk

# Create main window
root = Tk()
root.title("Image Viewer")
root.geometry("800x600")

# Image list and index
image_list = []
image_index = 0

# Function to load and show an image
def load_image():
    global image_index
    try:
        image_path = image_list[image_index]
        image = Image.open(image_path)
        image.thumbnail((600, 600))
        photo = ImageTk.PhotoImage(image)

        # Create a label to display the image
        image_label.config(image=photo)
        image_label.image = photo
    except IndexError:
        pass

# Function to load the images from a folder
def load_images():
    global image_list
    folder_selected = filedialog.askdirectory()
    if folder_selected:
        image_list = [os.path.join(folder_selected, f) for f in os.listdir(folder_selected)
                      if f.lower().endswith(('.png', '.jpg', '.jpeg', '.gif'))]
        if image_list:
            load_image()

# Function to navigate to the next image
def next_image():
    global image_index
    if image_index < len(image_list) - 1: image_index += 1 load_image() # Function to navigate to the previous image def previous_image(): global image_index if image_index > 0:
        image_index -= 1
        load_image()

# Create the UI components
load_button = Button(root, text="Load Images", command=load_images)
load_button.pack()

previous_button = Button(root, text="Previous", command=previous_image)
previous_button.pack(side=LEFT, padx=10)

next_button = Button(root, text="Next", command=next_image)
next_button.pack(side=RIGHT, padx=10)

# Label to display the image
image_label = Label(root)
image_label.pack()

# Run the app
root.mainloop()

Explanation of the Program

This program creates a basic image viewer using Python and the Tkinter library. Here’s a breakdown of how it works:

  • Import Libraries: The program imports the Tkinter library for creating the graphical interface, and PIL (Python Imaging Library) to handle image operations.
  • Window Setup: A Tkinter window is created with a title and dimensions of 800×600 pixels.
  • Image List: We define a list to hold the image paths, and an index to track the current image.
  • Load Images Function: The user can select a folder containing images using a file dialog. The app will filter the files to include only supported image types (.png, .jpg, .jpeg, .gif).
  • Displaying Images: The selected image is resized (thumbnail) and displayed on the Tkinter window using a Label widget.
  • Navigation: Two buttons allow the user to navigate through the images (previous and next).

How to Run the Program

Follow these steps to run the Image Viewer app:

  1. Ensure you have Python installed on your computer (Python 3.x recommended).
  2. Install the required libraries by running: pip install Pillow to install the Python Imaging Library (PIL).
  3. Copy the provided code into a Python file (e.g., image_viewer.py).
  4. Run the program using the command: python image_viewer.py.
  5. The app will open a window where you can load a folder of images, and then navigate through them using the Previous and Next buttons.
© 2025 Learn Programming. All rights reserved.

 

Leave a Reply

Your email address will not be published. Required fields are marked *