Read an image with OpenCV and display it with Tkinter

OpenCV is an Open Source Computer Vision library in Python which is widely used for Research purposes in Artificial Intelligence and Machine Learning. Computer Vision Library such as OpenCV deals with image processing. We can use OpenCV to read an image and further use it for development.

Let us suppose that we want to create an application that reads an image and displays it in the window using OpenCV with Tkinter as the GUI framework.

Installation

Install OpenCV by using the following command ?

pip install opencv-python

Also install Pillow for image processing ?

pip install pillow

Steps to Display Image

Follow these steps to read and display an image ?

  • Install OpenCV in the environment and import the library using import cv2.

  • Import NumPy and PIL (Pillow Package) for image calculation.

  • Load the Image using imread(image_location) function.

  • Split the RGB Color of the image using the split(image) function.

  • Merge the Image colors using merge(rgb) function.

  • Convert the multidimensional matrix into an image.

  • Convert the given image using PhotoImage(image= file) function.

  • Initialize a label and display the Image.

Example

Here's a complete example that demonstrates reading an image with OpenCV and displaying it with Tkinter ?

# Import the tkinter library
from tkinter import *
import numpy as np
import cv2
from PIL import Image, ImageTk

# Create an instance of tkinter frame
win = Tk()
win.title("OpenCV Image Display")
win.geometry("700x550")

# Load the image
img = cv2.imread('tutorialspoint.png')

# Check if image was loaded successfully
if img is not None:
    # Rearrange colors from BGR to RGB
    blue, green, red = cv2.split(img)
    img = cv2.merge((red, green, blue))
    
    # Convert to PIL Image
    im = Image.fromarray(img)
    imgtk = ImageTk.PhotoImage(image=im)
    
    # Create a Label to display the image
    Label(win, image=imgtk).pack(pady=20)
else:
    # Display error message if image not found
    Label(win, text="Image not found! Please check the file path.", 
          font=("Arial", 16), fg="red").pack(pady=100)

win.mainloop()

How It Works

OpenCV reads images in BGR (Blue-Green-Red) format by default, while most display systems expect RGB format. The cv2.split() and cv2.merge() functions rearrange the color channels from BGR to RGB format before displaying.

The PIL Image.fromarray() converts the NumPy array to a PIL Image object, and ImageTk.PhotoImage() converts it to a format compatible with Tkinter widgets.

Output

Running the above code will load and display an image in the Tkinter window. Make sure the image 'tutorialspoint.png' is located in the same folder as the program.

Tkinter Window Your Image Displayed Here

Key Points

  • OpenCV reads images in BGR format, so color channel conversion is needed for proper display

  • PIL is used as an intermediate step to convert OpenCV arrays to Tkinter-compatible format

  • Always check if the image was loaded successfully to avoid runtime errors

  • Keep the image file in the same directory as your Python script for easy access

Conclusion

Using OpenCV with Tkinter allows you to create desktop applications that can display and manipulate images. The key is converting BGR to RGB format and using PIL as a bridge between OpenCV and Tkinter.

Updated on: 2026-03-25T20:41:13+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements