Ask Your Question
0

cv2.imread() gives None, but the path/cwd is correct

asked 2020-07-16 16:02:58 -0600

Oregano Hauch gravatar image

updated 2020-07-17 02:37:28 -0600

Hello,

I had this problem already and it was solved without any clear reason. I had my Jupyter Notebook tab open and didn't change anything and then it worked. I already read articles and other threads on this, but couldn't find a solution for this; just look at my screenshot:

PATH = "savefig/plotXBIC_singlecell/01.png"
img = cv2.imread(PATH)
print(img)
print(path.exists(PATH))

None True

image description

On a PyImageSearch blog post I read that this can happen with bad compiling, such as JPEGs. But this doesn't make sense: It already worked a few hours ago and I could display the img with plt.imshow(). (Not with cv2.imshow(), that leads to my kernel crashing.)

edit retag flag offensive close merge delete

Comments

1

Check also is path is regular file and can be read. Also as you talking about Jupyter Notebook - this is not a "regular" enviroment. Most likely the cause is there.

holger gravatar imageholger ( 2020-07-16 16:21:36 -0600 )edit

be so kind to us, and replace the screenshot with a text version of your code, thank you.

berak gravatar imageberak ( 2020-07-17 01:31:14 -0600 )edit

also, relative paths are relative to where you start your program, that's not nessecarily, where your script is.

try with an absolute path instead. also check your image with another program (maybe it's a misnamed .gif)

berak gravatar imageberak ( 2020-07-17 01:33:51 -0600 )edit

I changed the PATH to the absolute path and nothing changed. os.path.isfile(PATH) gives me True.

Oregano Hauch gravatar imageOregano Hauch ( 2020-07-17 02:59:19 -0600 )edit

If it worked a couple of hours ago, but not now, something has changed - perhaps crashed. It is not uncommon to debug a problem for hours only to see the problem disappear when the computer is restarted...

mvuori gravatar imagemvuori ( 2020-07-17 03:38:44 -0600 )edit

I restarted the computer, I restarted the kernel... I also placed the 01.png at my cwd... what else can I try?

Oregano Hauch gravatar imageOregano Hauch ( 2020-07-17 04:28:16 -0600 )edit

please check if it is really a valid png image

maybe: head 01.png and see if the file magic is correct (again, gif ? svg ?)

it worked a couple of hours ago

seriously, .....

berak gravatar imageberak ( 2020-07-17 04:38:59 -0600 )edit

Sorry, I don't understand your comment. Could you rephrase it, please? Something about your syntax is confusing. (Dots after comma, citation of mvuori's comment, but only part of it, and what do you mean by "valid png"? Do you mean if it is an actual png and not just its file name? Yes, it is, I created it with matplotlib's savepng.)

Oregano Hauch gravatar imageOregano Hauch ( 2020-07-17 15:48:11 -0600 )edit

??? I still didn't solve this problem...

Oregano Hauch gravatar imageOregano Hauch ( 2020-07-20 04:04:36 -0600 )edit

Can nobody here help? :-(

Oregano Hauch gravatar imageOregano Hauch ( 2020-07-21 15:28:04 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2020-07-22 23:25:15 -0600

Oregano Hauch gravatar image

Okay, I solved it. The PNGs were empty for some reason (Zero bytes). I don't know exactly how this could happen... probably because I created them with the copyfile function which I replaced with the shutil.copy function now.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-07-16 16:02:58 -0600

Seen: 8,033 times

Last updated: Jul 22 '20