Cv2.imread always returns NoneType

cv2.imread always returns NoneType .

I am using python version 2.7 and OpenCV 2.4.6 on 64 bit Windows 7.

Perhaps this is some kind of error or permission, because the exact same installation of the python and cv2 packages on another computer works correctly. Here is 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 key would be appreciated.

+4
source share
9 answers

Perhaps this will be an OpenCV error that is not yet resolved. cv2.imread also does not work under Win32 for me.

At the same time, use LoadImage, which should work fine.

 im = cv2.cv.LoadImage("D:\testdata\some.tif",CV_LOAD_IMAGE_COLOR) 
+3
source

Try changing the direction of the slash

 im = cv2.imread("D:/testdata/some.tif",CV_LOAD_IMAGE_COLOR) 

or add r to the beginning of the line

 im = cv2.imread(r"D:\testdata\some.tif",CV_LOAD_IMAGE_COLOR) 
+5
source

just stumbled upon this one.

The solution is very simple, but not intuitive.

  • If you use relative paths, you can use either "\" or "/", as in test\pic.jpg or test/pic.jpg respectively
  • If you use absolute paths, you should only use "/", as in /.../test/pic.jpg for unix or C:/.../test/pic.jpg for windows
  • to be safe, use for root, _, files in os.walk(<path>): in combination with abs_path = os.path.join(root, file) . Calling imread after this, as in img = ocv.imread(abs_path) , will always work.
+2
source
 >>> im=cv2.imread("C:\Users\Virgile\Downloads\red.JPG") >>> print im None >>> im=cv2.imread("C:/Users/Virgile/Downloads/red.JPG") >>> print im [[[ 15 36 51] [ 18 34 51] [ 19 33 51] ..., 

(verified using opencv 3.0.0)

+1
source

It took a long time. first make sure the file is in the directory and make sure that even though Windows Explorer says that the “JPEG” file is actually “JPG”. The first print statement is key to the fact that the file does exist. I am a beginner, so if the code sucks, so be it. The code simply imports the image 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 
0
source

I had a similar problem, the direction of the slashes was changing

0
source

I spent some time on this only to discover that this error was caused by a broken image file in my case. Therefore, please manually check your file to make sure it is valid and can be opened by ordinary image viewers.

0
source

In my case, the problem was gaps in the path. After I moved the images to the path without spaces, it worked.

0
source

I came across this. It turns out that the PIL module provides this functionality. Similarly, numpy.imread and scipy.misc.imread both did not exist until I installed PIL

In my configuration (win7 python2.7) this was done as follows:

 cd /c/python27/scripts easy_install PIL 
-one
source

Source: https://habr.com/ru/post/1494304/


All Articles