OpenCV Python not opening images with imread()
Question:
I’m not entirely sure why this is happening but I am in the process of making a program and I am having tons of issues trying to get opencv to open images using imread. I keep getting errors saying that the image is 0px wide by 0px high. This isn’t making much sense to me so I searched around on here and I’m not getting any answers from SO either.
I have taken about 20 pictures and they are all using the same device. Probably 8 of them actually open and work correctly, the rest don’t. They aren’t corrupted either because they open in other programs. I have triple checked the paths and they are using full paths.
Is anyone else having issues like this? All of my files are .jpgs and I am not seeing any problems on my end. Is this a bug or am I doing something wrong?
Here is a snippet of the code that I am using that is reproducing the error on my end.
imgloc = "F:KyleDesktopCoinjarTest imagesten.png"
img = cv2.imread(imgloc)
cv2.imshow('img',img)
When I change the file I just adjust the name of the file itself the entire path doesn’t change it just refuses to accept some of my images which are essentially the same ones.
I am getting this error from a later part of the code where I try to use img.shape
Traceback (most recent call last):
File "F:KyleDesktopCoinjarCoinJar Test2.py", line 14, in <module>
height, width, depth = img.shape
AttributeError: 'NoneType' object has no attribute 'shape'
and I am getting this error when I try to show a window from the code snippet above.
Traceback (most recent call last):
File "F:KyleDesktopCoinjarCoinJar Test2.py", line 11, in <module>
cv2.imshow('img',img)
error: ........opencvmoduleshighguisrcwindow.cpp:261: error: (-215) size.width>0 && size.height>0 in function cv::imshow
Answers:
Probably you have problem with special meaning of
in text – like t
or n
Use \
in place of
imgloc = "F:\Kyle\Desktop\Coinjar\Test images\ten.png"
or use prefix r''
(and it will treat it as raw
text without special codes)
imgloc = r"F:KyleDesktopCoinjarTest imagesten.png"
EDIT:
Some modules accept even /
like in Linux path
imgloc = "F:/Kyle/Desktop/Coinjar/Test images/ten.png"
Take care to :
- try
imread()
with a reliable picture,
- and the correct path in your context like (see Kyle772 answer). For me either
//
or
.
I lost a couple of hours trying with 2 images saved from a left click in a browser. As soon as I took a personal camera image, it works fine.
#context windows10 / anaconda / python 3.2.0
import cv2
print(cv2.__version__) # 3.2.0
imgloc = "D:/violettes/Software/Central/test.jpg" #this path works fine.
# imgloc = "D:\violettes\Software\Central\test.jpg" this path works fine also.
#imgloc = "D:violettesSoftwareCentraltest.jpg" #this path fails.
img = cv2.imread(imgloc)
height, width, channels = img.shape
print (height, width, channels)
When you get error like this AttributeError: ‘NoneType’ object has no attribute ‘shape’
Try with new_image=image.copy
My suggestion for everyone facing the same problem is to try this:
cv2.imshow("image", img)
The img
is keyword. Never forget.
From my experience, file paths that are too long (OS dependent) can also cause cv2.imread()
to fail.
Also, when it does fail, it often fails silently, so it is hard to even realize that it failed, and usually something further the the code will be what sparks the error.
Hope this helps.
I had i lot of trouble with cv.imread()
not finding my Image. I think i tryed everything involving changing the path. The os.path.exists(file_path)
function also gave me back a True.
I finaly solved the problem by loading the images with imageio
.
img = imageio.imread('file_path')
This also loads the img in a numpy array and you can use funktions like cv.matchTemplate()
on this object. But i would recomment if u are doing stuff with multiple images that you then read all of them with imageio because i found diffrences in the arrays produced by .imread()
from the two libs (opencv, imageio) on a File both of them could open.
I hope i could help someone
Faced the same problem on Windows: cv.imread
returned None
when reading jpg files from a subfolder. The same code and folder structure worked on Linux.
Found out that cv.imread
processes the same jpg files, if they are in the same folder as the python file.
My workaround:
- copy the image file to the python file folder
- use this file in
cv.imread
- remove redundant image file
import os
import shutil
import cv2 as cv
image_dir = os.path.join('path', 'to', 'image')
image_filename = 'image.jpg'
full_image_path = os.path.join(image_dir, image_filename)
image = cv.imread(full_image_path)
if image is None:
shutil.copy(full_image_path, image_filename)
image = cv.imread(image_filename)
os.remove(image_filename)
...
I know that the question is already answered but in case anybody still is not able to load images with imread. It may be because there are letters in the string path witch imread does not accept.
For exmaple umlauts and diacritical marks.
I’m not entirely sure why this is happening but I am in the process of making a program and I am having tons of issues trying to get opencv to open images using imread. I keep getting errors saying that the image is 0px wide by 0px high. This isn’t making much sense to me so I searched around on here and I’m not getting any answers from SO either.
I have taken about 20 pictures and they are all using the same device. Probably 8 of them actually open and work correctly, the rest don’t. They aren’t corrupted either because they open in other programs. I have triple checked the paths and they are using full paths.
Is anyone else having issues like this? All of my files are .jpgs and I am not seeing any problems on my end. Is this a bug or am I doing something wrong?
Here is a snippet of the code that I am using that is reproducing the error on my end.
imgloc = "F:KyleDesktopCoinjarTest imagesten.png"
img = cv2.imread(imgloc)
cv2.imshow('img',img)
When I change the file I just adjust the name of the file itself the entire path doesn’t change it just refuses to accept some of my images which are essentially the same ones.
I am getting this error from a later part of the code where I try to use img.shape
Traceback (most recent call last):
File "F:KyleDesktopCoinjarCoinJar Test2.py", line 14, in <module>
height, width, depth = img.shape
AttributeError: 'NoneType' object has no attribute 'shape'
and I am getting this error when I try to show a window from the code snippet above.
Traceback (most recent call last):
File "F:KyleDesktopCoinjarCoinJar Test2.py", line 11, in <module>
cv2.imshow('img',img)
error: ........opencvmoduleshighguisrcwindow.cpp:261: error: (-215) size.width>0 && size.height>0 in function cv::imshow
Probably you have problem with special meaning of in text – like
t
or n
Use \
in place of
imgloc = "F:\Kyle\Desktop\Coinjar\Test images\ten.png"
or use prefix r''
(and it will treat it as raw
text without special codes)
imgloc = r"F:KyleDesktopCoinjarTest imagesten.png"
EDIT:
Some modules accept even /
like in Linux path
imgloc = "F:/Kyle/Desktop/Coinjar/Test images/ten.png"
Take care to :
- try
imread()
with a reliable picture, - and the correct path in your context like (see Kyle772 answer). For me either
//
or.
I lost a couple of hours trying with 2 images saved from a left click in a browser. As soon as I took a personal camera image, it works fine.
#context windows10 / anaconda / python 3.2.0
import cv2
print(cv2.__version__) # 3.2.0
imgloc = "D:/violettes/Software/Central/test.jpg" #this path works fine.
# imgloc = "D:\violettes\Software\Central\test.jpg" this path works fine also.
#imgloc = "D:violettesSoftwareCentraltest.jpg" #this path fails.
img = cv2.imread(imgloc)
height, width, channels = img.shape
print (height, width, channels)
When you get error like this AttributeError: ‘NoneType’ object has no attribute ‘shape’
Try with new_image=image.copy
My suggestion for everyone facing the same problem is to try this:
cv2.imshow("image", img)
The img
is keyword. Never forget.
From my experience, file paths that are too long (OS dependent) can also cause cv2.imread()
to fail.
Also, when it does fail, it often fails silently, so it is hard to even realize that it failed, and usually something further the the code will be what sparks the error.
Hope this helps.
I had i lot of trouble with cv.imread()
not finding my Image. I think i tryed everything involving changing the path. The os.path.exists(file_path)
function also gave me back a True.
I finaly solved the problem by loading the images with imageio
.
img = imageio.imread('file_path')
This also loads the img in a numpy array and you can use funktions like cv.matchTemplate()
on this object. But i would recomment if u are doing stuff with multiple images that you then read all of them with imageio because i found diffrences in the arrays produced by .imread()
from the two libs (opencv, imageio) on a File both of them could open.
I hope i could help someone
Faced the same problem on Windows: cv.imread
returned None
when reading jpg files from a subfolder. The same code and folder structure worked on Linux.
Found out that cv.imread
processes the same jpg files, if they are in the same folder as the python file.
My workaround:
- copy the image file to the python file folder
- use this file in
cv.imread
- remove redundant image file
import os
import shutil
import cv2 as cv
image_dir = os.path.join('path', 'to', 'image')
image_filename = 'image.jpg'
full_image_path = os.path.join(image_dir, image_filename)
image = cv.imread(full_image_path)
if image is None:
shutil.copy(full_image_path, image_filename)
image = cv.imread(image_filename)
os.remove(image_filename)
...
I know that the question is already answered but in case anybody still is not able to load images with imread. It may be because there are letters in the string path witch imread does not accept.
For exmaple umlauts and diacritical marks.