Display image in Terminal using Python

In this article, we have explained the technique to display image in Terminal in Python using OpenCV and Python Imaging Library (PIL).

Table of contents:

  1. Load and display image in Python and OpenCV
  2. Complete Python Code

Load and display image in Python and OpenCV

Import relevant libraries like OpenCV and PIL (Python Image Library).

import cv2
from PIL import Image

OpenCV will be used to load a local image from the system while PIL will be used to display the image from the terminal.

To load a local image say "image.jpeg", use the following code:

img = cv2.imread("image.jpeg")

Convert the image to PIL form from the previous image array:

img = Image.fromarray(img, "RGB")

We have specified the color coding of the image that is RGB.

To display the image from the terminal, the command is:

img.show()

This will open a new terminal window that will display the local image. This is the easiest approach to display image.

Complete Python Code

Following is the complete Python code to Load and display a local image in Python and OpenCV:

import cv2
from PIL import Image

img = cv2.imread("image.jpeg")
img = Image.fromarray(img, "RGB")
img.show()

With this article at OpenGenus, you must have the complete idea of how to Load and display image in Python and OpenCV.