Shani Sadeh
Shani Sadeh

Reputation: 33

unable to load image with cv2.imread

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)

Upvotes: 3

Views: 2689

Answers (3)

I was facing the same problem but I have figured out the solution.

My approach: I placed the image to be used within the same folder as my Python script and passed only the name of the image without any extension. I also tested passing the absolute path of the image that is not within the same directory as the Python script and it works fine. The main point is to exclude the image extension. I don't have the technical explanation as to why this is so but I think it is implicitly handled by opencv.

img = imread('imagename') //no extension of the image
enter code herecv2.imshow('Window Name', img)

Note: I am running this using the Thonny IDE on my Raspberry Pi 3. Though this shouldn't be a problem I thought to provide as much information as possible that will help you resolve your underlying problem.

Hope this helps!

Upvotes: 0

Sreevathsabr
Sreevathsabr

Reputation: 689

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

Upvotes: 0

Teejay Bruno
Teejay Bruno

Reputation: 2159

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!

Upvotes: 0

Related Questions