Sai A Sai A
Updated date Feb 27, 2024
In this blog, we will learn how to create a basic image viewer in Python using simple methods like PIL, OpenCV, and Tkinter. Explore step-by-step instructions with code examples to view images easily.

Introduction:

In today's world, images are everywhere. Whether it's browsing through personal photos or analyzing data visualizations, having a tool to view images efficiently is essential. In this blog, we will explore how to create a basic image viewer using Python. We explore multiple methods, each offering a unique approach to image viewing, from the simplest to more advanced techniques.

Method 1: Using PIL (Python Imaging Library)

Python Imaging Library (PIL) is a powerful library for opening, manipulating, and saving many different image file formats. We'll start by using PIL to create a basic image viewer.

from PIL import Image

def basic_image_viewer(image_path):
    try:
        img = Image.open(image_path)
        img.show()
    except IOError:
        print("Unable to load image")

if __name__ == "__main__":
    image_path = "example_image.jpg"
    basic_image_viewer(image_path)

Output:

Upon running the program, the image "example_image.jpg" will be displayed using the default image viewer associated with your operating system.

In this method, we import the Image class from the PIL library. We define a function basic_image_viewer() that takes the path of the image file as input. Inside the function, we attempt to open the image using Image.open() and then display it using the show() method. If there's an error loading the image, we catch the IOError exception and print a message.

Method 2: Using OpenCV

OpenCV (Open Source Computer Vision Library) is a popular library for computer vision and image processing tasks. We can leverage OpenCV to create an image viewer with additional functionalities.

import cv2

def opencv_image_viewer(image_path):
    try:
        img = cv2.imread(image_path)
        cv2.imshow("Image Viewer", img)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
    except Exception as e:
        print("Unable to load image:", e)

if __name__ == "__main__":
    image_path = "example_image.jpg"
    opencv_image_viewer(image_path)

Output:

Running this program will display the image "example_image.jpg" in a window titled "Image Viewer".

In this method, we import the cv2 module from the OpenCV library. We define a function opencv_image_viewer() that takes the image path as input. Inside the function, we use cv2.imread() to read the image, cv2.imshow() to display it in a window, cv2.waitKey(0) to wait for a key press, and cv2.destroyAllWindows() to close all OpenCV windows. If an exception occurs, we catch it and print an error message.

Method 3: Using Tkinter

Tkinter is Python's standard GUI (Graphical User Interface) toolkit. We can utilize Tkinter to create a simple image viewer with a basic user interface.

import tkinter as tk
from PIL import ImageTk, Image

def tkinter_image_viewer(image_path):
    root = tk.Tk()
    root.title("Image Viewer")

    img = Image.open(image_path)
    img = ImageTk.PhotoImage(img)

    panel = tk.Label(root, image=img)
    panel.pack(side="top", fill="both", expand="yes")

    root.mainloop()

if __name__ == "__main__":
    image_path = "example_image.jpg"
    tkinter_image_viewer(image_path)

Output:

Executing this script will launch a window titled "Image Viewer" displaying the image "example_image.jpg".

In this method, we import tkinter for creating the GUI. We define a function tkinter_image_viewer() that takes the image path as input. Inside the function, we create a Tkinter window (root) with the title "Image Viewer". We open the image using PIL, convert it to a Tkinter-compatible format using ImageTk.PhotoImage(), and then display it in a Label widget within the Tkinter window.

Conclusion:

In this blog, we have covered three different methods to create a basic image viewer in Python. We started with the simplest approach using PIL, then progressed to using OpenCV for additional functionalities, and finally, we used Tkinter to create a GUI-based image viewer. 

Comments (0)

There are no comments. Be the first to comment!!!