3

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)
martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    First you shall confirm if the path is correct using `ls -la /Users/snysdh/Desktop/A1_Combined_T01.png` – ZdaR Apr 11 '20 at 05:22
  • 1
    There is a small chance that the file is corrupted (or non-standard PNG format), or your python script doesn't have read permissions to the file. Try other file in other path (same path as the Python file is a good suggestion). – Rotem Apr 11 '20 at 12:56

2 Answers2

0

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!

Teejay Bruno
  • 1,716
  • 1
  • 4
  • 11
0

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

Sreevathsabr
  • 649
  • 7
  • 23