Show Image | OpenCV vs Pillow | Python

This tutorial will show you how to display/show an image using OpenCV (cv2) and Pillow (PIL). You might also want to learn how to read/open images using cv2 and PIL.

Show Image

Pillow

pil_img.show()

OpenCV

The cv2.waitKey(0) is used to prevent the window from being killed instantly.

cv2.imshow("Image Title", cv2_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Full Example

Pillow

from PIL import Image

# read image
pil_img = Image.open("test_images/test1.jpg")

# show image
pil_img.show()

OpenCV

import cv2

# read image
cv2_img = cv2.imread("test_images/test1.jpg")

# show image
cv2.imshow("Image Title", cv2_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Syntax

Pillow

Image.show(title=None)

Parameters:

  • title: Optional title to use for the image window, where possible.

Returns:

  • None

OpenCV

cv2.imshow(winname, mat)

Parameters:

  • winname: Name of the window.
  • mat: Image to be shown.

Returns:

  • None

References

Avatar photo
Steins

Developer & AI Researcher. Write about AI, web dev/hack.