Site icon TechVidvan

Python Image Viewer using Tkinter

Image Viewer App is an application that will display images and have a forward and backward button to go back and forth and view different photos one by one. Let us create an Image Viewer App Project in Python in easy steps.

About Image Viewer App

An Image Viewer App is an application that helps us display images one by one. Here we can go back and forth while viewing the images. The size of these images is random and is displayed accordingly.

Project Details

In this project, we are going to create an Image Viewer App that uses the Tkinter Module. We are also going to use the PIL Module to do operations with the images. We will ensure that a user can use the application to browse through a series of images.

Project Prerequisites

To build this project, we will require Tkinter Module and PIL Module. We will also require some basic knowledge of Python to build this project.

Download Python Image Viewer Code

please download the source code of the Image Viewer App Project from the following link: Image Viewer App in Python Code

Steps to Create Python Image Viewer Project

We will be going through the following steps:

  1. Importing the Required Modules
  2. Creating the GUI
  3. Making a list of images
  4. Next and Previous Image

Let’s start looking at each step in detail.

1. Importing the Required Modules

from tkinter import *
import tkinter as tk
from PIL import ImageTk, Image

We import these modules to use them to build our project.

2. Creating the GUI

window = Tk()#creating window
window.resizable()#geometry of window
window.title('TechVidvan')#title to window
Label(window,text="Image Viewer App",font=('bold',20)).pack()#label
 
#creating frame
frame=Frame(window, width=230, height=200, bg='white')
frame.pack()
#creating buttons
Button(window,text='Back',command=Back,bg='light blue').place(x=230,y=40)
Button(window,text='Next',command=Next,bg='light blue').place(x=1000,y=40)

1. Back Button- To view the previous image.

2. Next Button – To view the next image.

3. Creating Image List:

#opening images
image1=ImageTk.PhotoImage(Image.open("image1.jpeg"))
image2=ImageTk.PhotoImage(Image.open("image2.jpg"))
image3=ImageTk.PhotoImage(Image.open("image3.jpeg"))
 
#adding images to list
lst=[image1,image2,image3]
i = 0
image_label = Label(frame, image=lst[i])#packing image into the window
image_label.pack()

4. Next and Previous Image:

def Next():
    global i
    i = i + 1
    try:
        image_label.config(image=lst[i])
    except:
        i = -1
        Next()
def Back():
    global i
    i = i - 1
    try:
        image_label.config(image=lst[i])
    except:
        i = 0
        Back()

Python Image Viewer Output

Let us look at the output of python image viewer project.

Summary

Hurray! We have successfully created the Image Viewer App using python. We have used two Modules to build this project-

Exit mobile version