unable to load image with cv2.imread

Viewed 1075

I am trying to load an image using cv2.imread, but keep getting this error

error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'

I googled the error and it seems that most of the time the problem is related to misspelling the file name/path. However, I copied the file path using the "copy path" option on mac. What can be the problem?

My code:

import cv2
import numpy as np
from matplotlib import pyplot as plt
from scipy import ndimage
from skimage import measure, color, io

path='/Users/snysdh/Desktop/A1_Combined_T01.png'
img1=cv2.imread(path)

cv2.imshow("Red Image", img1)
2 Answers

The problem is either related to your path or the image.

As a sanity check try making a copy of the image and putting it in the same folder as your script, then change the path var to the name of your copy path='copy.png'

At the very least this should work. Hope it helps!

I think you need to do some small checks here. Copy the path which is been provided in path and try to open in File explorer to check the does image really exists at the path which is been provided

path='/Users/snysdh/Desktop/A1_Combined_T01.png'

If exists the, we need to use the // in place fo / as there are times / with character around might be treated as Special character. Or we need to write r to make it a regular expression to avoid special character problem

Related