Image is not displaying in Google Colab while using imshow()

Question:

I am working on a project which requires functions from OpenCV to plot images.
I am trying to display image using the below code in Google Colab. But nothing shows up in the output. Can anybody help me with this?

%pylab notebook
import cv2

testim = imread('butterfly.jpg')
figure()
imshow(testim)
plt.show()

Screenshot:

enter image description here

Link to my Colab Notebook

Asked By: Alankrit

||

Answers:

imshow requires an X server, which isn’t available in a web browser.

Instead, use the IPython.display.Image library. Here’s an example:
https://colab.research.google.com/drive/1jWHKR6rhhyZtUulttBD6Pxd_AJhgtVaV
enter image description here

Answered By: Bob Smith

Found one workaround. We can use %matplotlib inline in the code to use . Used as example here in In[28] – link

Answered By: Alankrit

Google colab crashes if you try to display image using cv2.imshow() instead import from google.colab.patches import cv2_imshow and display using cv2_imshow(<image>)

Answered By: Mohammed Waseem

The cv2.imshow() and cv.imshow() functions from the opencv-python package are incompatible with Jupyter notebook; see https://github.com/jupyter/notebook/issues/3935.

As a replacement, you can use the following function:

from google.colab.patches import cv2_imshow

For example, here we download and display a PNG image of the Colab logo:

!curl -o logo.png https://colab.research.google.com/img/colab_favicon_256px.png
import cv2
img = cv2.imread('logo.png', cv2.IMREAD_UNCHANGED)
cv2_imshow(img)

Credits: Code Snippets in Google Colab

Answered By: Ajai

Instead of using cv2.imshow() try this:

  1. Change the import to from google.colab.patches import cv2_imshow
  2. Replace cv2.imshow() to cv2_imshow()

I tried it and it worked for me.

Answered By: Abhay Jain
cv2.imshow() 

does not work well in colab, you can use matplotlib for displaying.

import matplotlib.image as mpimg 
from matplotlib.pyplot import imshow
%matplotlib inline
testim = mpimg.imread('butterfly.jpg')
imshow(testim)

or you can do colab’s own cv2_imshow version

from google.colab.patches import cv2_imshow
cv2_imshow('butterfly.jpg')
Answered By: Muhammad Zakaria
from google.colab.patches import cv2_imshow
image = cv2.imread("image.png")
cv2_imshow(image)
Answered By: ravindra

I am also facing a same issue in google colab.

We can use cv2_imshow() instead of (cv2.imshow or cv.imshow):

#We must import first line of code
**#working module**

from google.colab.patches import cv2_imshow
import cv2 as cv
#Replace cv2.imshow() to cv2_imshow()
img = cv.imread('python.jpg') #mentioning a path of an image
cv2_imshow(img)

while we are using our local machine to visble any image then we use this code cv.imshow(), but when it comes to google colab we should switch to alternative code cv2_imshow()

example

Answered By: Rabiyulfahim
import os
import numpy as np
import matplotlib.pyplot as plt
import cv2
import pandas as pd
import matplotlib.image as mpimg
from google.colab.patches import cv2_imshow 

cv2_imshowcv21_image =cv2.imread("apple.jpg")
scale_percent = 20 # percent of original size
width = int(cv21_image.shape[1] * scale_percent / 100)
height = int(cv21_image.shape[0] * scale_percent / 100)
dim = (width, height)
resized=cv2.resize(cv21_image,dim)
cv2_imshow(resized)
cv2.waitKey(0)

The modified code do the process.

Answered By: Am_official