Reputation: 20633
cv2.imread
is always returning NoneType
.
I am using python version 2.7 and OpenCV 2.4.6 on 64 bit Windows 7.
Maybe it's some kind of bug or permissions issue because the exact same installation of python and cv2 packages in another computer works correctly. Here's the code:
im = cv2.imread("D:\testdata\some.tif",CV_LOAD_IMAGE_COLOR)
I downloaded OpenCV from http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv. Any clue would be appreciated.
Upvotes: 33
Views: 104997
Reputation: 2575
Sometimes the file is corrupted. If it exists and cv2.imread returns None this may be the case.
Try opening the file כfrom file explorer and see if that works
Upvotes: 0
Reputation: 1275
In my case helped changing file names to latin alphabet.
Instead of renaiming all files I wrote a simple wrapper to rename a file before the load into a random guid and right after the load rename it back.
import os
import uuid
import cv2
uid = str(uuid.uuid4())
def wrap_file_rename(my_path, function):
try:
directory = os.path.dirname(my_path)
new_full_name = os.path.join(directory, uid)
os.rename(my_path, new_full_name)
return function(new_full_name)
except Exception as error:
logger.error(error) # use your logger here
finally:
os.rename(new_full_name, my_path)
def my_image_read(my_path, param=None):
return wrap_file_rename(my_path, lambda p: cv2.imread(p) if param is None else cv2.imread(p, param))
Upvotes: 0
Reputation: 1
I had a similar issue,changing direction of slashes worked:
Change /
to \
Upvotes: 0
Reputation: 63
My OS is Windows 10. I noticed imread is very sensitive to path. No any recommendation about slashes worked for me, so how I managed to solve problem: I have placed file to project folder and typed: img = cv2.imread("MyImageName.jpg", 0)
So without any path and folder, just file name. And that worked for me. Also try different files from different sources and of different formats
Upvotes: 2
Reputation: 824
In case no one mentioned in this question, another way to workaround is using plt
to read image, then convert it to BGR
format.
img=plt.imread(img_path)
print(img.shape)
img=img[...,::-1]
it has been mentioned in cv2.imread does not read jpg files
Upvotes: 6
Reputation: 2117
I also met the same issue before on ubuntu 18.04.
cv2.imread(path)
I solved it when I changed the path
argument from Relative_File_Path
to Absolute_File_Path
.
Hope it be useful.
Upvotes: 10
Reputation: 41
I had a similar problem, changing the name of the image to English alphabetic worked for me. Also, it didn't work with a numeric name (e.g. 1.jpg).
Upvotes: 1
Reputation: 11232
First, make sure the path is valid, not containing any single backslashes. Check the other answers, e.g. https://stackoverflow.com/a/26954461/463796.
If the path is fixed but the image is still not loading, it might indeed be an OpenCV bug that is not resolved yet, as of 2013. cv2.imread
is not working properly under Win32 for me either.
In the meantime, use LoadImage, which should work fine.
im = cv2.cv.LoadImage("D:/testdata/some.tif", CV_LOAD_IMAGE_COLOR)
Upvotes: 14
Reputation: 16216
In my case the problem was the spaces in the path. After I moved the images to a path with no spaces it worked.
Upvotes: 10
Reputation: 1674
I spent some time on this only to find that this error is caused by a broken image file on my case. So please manually check your file to make sure it is valid and can be opened by common image viewers.
Upvotes: 0
Reputation: 738
just stumbled upon this one.
The solution is very simple but not intuitive.
test\pic.jpg
or test/pic.jpg
respectively/.../test/pic.jpg
for unix or C:/.../test/pic.jpg
for windowsfor root, _, files in os.walk(<path>):
in combination with abs_path = os.path.join(root, file)
. Calling imread afterwards, as in img = ocv.imread(abs_path)
is always going to work.Upvotes: 6
Reputation: 19
This took a long time to resolve. first make sure that the file is in the directory and check that even though windows explorer says the file is "JPEG" it is actually "JPG". The first print statement is key to making sure that the file actually exists. I am a total beginner, so if the code sucks, so be it. The code, just imports a picture and displays it . If the code finds the file, then True will be printed in the python window.
import cv2
import sys
import numpy as np
import os
image_path= "C:/python27/test_image.jpg"
print os.path.exists(image_path)
CV_LOAD_IMAGE_COLOR = 1 # set flag to 1 to give colour image
CV_LOAD_IMAGE_COLOR = 0 # set flag to 0 to give a grayscale one
img = cv2.imread(image_path,CV_LOAD_IMAGE_COLOR)
print img.shape
cv2.namedWindow('Display Window') ## create window for display
cv2.imshow('Display Window', img) ## Show image in the window
cv2.waitKey(0) ## Wait for keystroke
cv2.destroyAllWindows() ## Destroy all windows
Upvotes: 2
Reputation: 629
Try changing the direction of the slashes
im = cv2.imread("D:/testdata/some.tif",CV_LOAD_IMAGE_COLOR)
or add r to the begining of the string
im = cv2.imread(r"D:\testdata\some.tif",CV_LOAD_IMAGE_COLOR)
Upvotes: 8
Reputation: 343
I've run into this. Turns out the PIL module provides this functionality. Similarly, numpy.imread and scipy.misc.imread both didn't exist until I installed PIL
In my configuration (win7 python2.7), that was done as follows:
cd /c/python27/scripts
easy_install PIL
Upvotes: -1