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

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



Solution 1:[1]

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

Solution 2:[2]

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

Solution 3:[3]

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

Solution 4:[4]

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')

Solution 5:[5]

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.

Solution 6:[6]

from google.colab.patches import cv2_imshow

image = cv2.imread("image.png")

# "image.png" is image path.

cv2_imshow(image)

https://i.stack.imgur.com/53YPm.png

Solution 7:[7]

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

#Replace cv2.imshow() to cv2_imshow()
img = cv.imread('tiger.jpg') #mentioning a path of an image
cv2_imshow(img)
 #do not use below code it won't work for me also so i switch to above mentioned code to viewing an image
 #non working module, error code mentioning for your references.
 import cv2 as cv   
 img = cv.imread('tiger.jpg') #mentioning a path of an image
 cv.imshow(img)

Solution 8:[8]

You can use "import cv2 as cv" and do all the processing and while displaying image use

from google.colab.patches import cv2_imshow

cv2_imshow(image)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ajai
Solution 2 Alankrit
Solution 3 Bob Smith
Solution 4 Muhammad Zakaria
Solution 5 Pratham
Solution 6 ravindra
Solution 7 Rabiyulfahim
Solution 8 Sandhya Krishnan