Getting error when using cv2.imshow()

###here is my code :

import cv2

img = cv2.imread(‘dp.jpeg’)

cv2.imshow(‘my image’,img)

cv2.waitKey(0)


this error is coming in my terminal

Hey @Aayush, write this code in jupyter notebook, and check if it runs,

import cv2
import matplotlib.pyplot as plt
img = cv2.imread(‘dp.jpeg’)
plt.imshow(img)
plt.show()

This will make clear if path specified of dp.jpeg is correct or not. Also dp.jpeg must be in same folder as .py or .ipynb is in. If this worked, Than you just need to add cv2.destroyallwindows() at last of your code.

Hope this resolved your doubt. :blush:

@S18CRX0120
dp.jpeg and code file are in same folder and when i try to plot image using matplotlib then it is working fine but when i try to use :

import cv2

img = cv2.imread(‘dp.jpeg’)

cv2.imshow(‘my image’,img)

cv2.waitKey(0)

cv2.destroyAllWindows()

This causes same error that i have shared you

How you installed opencv??

@S18CRX0120
i used this to install opencv :
python -m pip install opencv-python

Hey @Aayush, are you using iTerm?. In case you are using iTerm, now try running same code on base terminal that comes by default with mac. iTerm sometimes mess things up.

@S18CRX0120
I m using basic terminal only. Also if i try to run this code on jupyter-notebook then also i get error due to which kernel restarts

Hey @Aayush, run this command and terminal
pip3 install opencv-python-headless

And try running it again.

1 Like

@S18CRX0120
It worked thanx :slight_smile:

Hey @Aayush, if your doubt is resolved completely, I request you to close the doubt and provide the feedback.
Happy Learning :blush:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.