Display CV2 Image in Jupyter/Google Colab

This following doesn't work as there is no x-window in Jupyter or Google Colab.

import cv2cv2.imshow("result", image)

Option 1: Google Colab

If you are using Google Colab

from google.colab.patches import cv2_imshowcv2_imshow(image)

NOTE: source code fro cv2_imshow

Option 2: IPython.display and PIL

from PIL import Imagefrom IPython.display import display, clear_output# convert color from CV2 BGR back to RGBimage = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)display(Image.fromarray(image))

Option 3: matplotlib.pyplot

import matplotlib.pyplot as plt# set sizeplt.figure(figsize=(10,10))plt.axis("off")# convert color from CV2 BGR back to RGBimage = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)plt.imshow(image)plt.show()

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.