0

EDIT 2:
Turns out the problem was that the path had Unicode symbols so the solution is in this post

EDIT 1:
Meanwhile, I found out that the problem is the combination of OpenCV + Spaces in Path + External Disk. Removing ANY of the elements solves the issue so the current target is just trying to find a solution for this combination.
OS is Windows

Problem:
I can't load images from my external HDD using Python-OpenCV.
I can use all the osand open Python functions such as read .TXT files and list/walk directories from the HDD but when I try to load images using cv2.imread I get a None object...
The file path is written in a Python-friendly way (using "\" instead of "") and it works for reading the image as a binary with open(path, "rb")

Already Tried:

  • r string r"TEXT"
  • Changing \\ to /
  • Double quotes "\"TEXT\"\"
  • os module os.path.normpath(PATH) and os.path.join([HALF, HALF])

I also read the OpenCV documentation and tried Google and StackOverflow but I don't find anyone with my problem so I'm kind of lost about what to try next...

import cv2
image = cv2.imread("G:\\Fotografias e Vídeos\\[...]\\11009982_1254240047936434_1963406974405452579_n.jpg")
print(image)
cv2.imshow("d", image)
# Output
None
Traceback (most recent call last):
  File "C:\Users\[NAME]\Documents\Python\test.py", line 4, in <module>
    cv2.imshow("d", image)
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'
  • 2
    What was your exact read command? Did you include the volume in the path? – fmw42 Mar 27 '20 at 04:37
  • `image = cv2.imread("G:\Fotografias e Vídeos\...\Fotografia")` – Vasco Cansado Carvalho Mar 28 '20 at 03:44
  • 1
    I am not a Windows user, but should it not be G:\\..... – fmw42 Mar 28 '20 at 04:07
  • Yes, I did use it as you said but I forgot that I had meanwhile changed to try the different possibility. I double checked now and there is the whole literal text I used and its output. Thanks for your quick answers – Vasco Cansado Carvalho Mar 28 '20 at 18:12
  • 1
    try reading a file that does not have spaces in the name. see if that works. what are the dimensions, number of channels and datatype for the file you are trying to read? Can you move the same file to your local disk and try reading it from there. – fmw42 Mar 28 '20 at 18:44
  • 1
    Using a path without spaces worked! Thanks a lot! – Vasco Cansado Carvalho Mar 28 '20 at 19:46
  • 1
    Paths with spaces need to be put inside double quotes. So you may be able to use them if you escape extra double quotes. Sorry, I am not a Windows user but try adding extra quotes that are escaped by ^ as: `cv2.imread("^"G:\\Fotografias e Vídeos\\Algarve\\Pedras del Rei 2015\\11009982_1254240047936434_1963406974405452579_n.jpg^"")` – fmw42 Mar 28 '20 at 20:40
  • oh boy what is this... NO, paths with spaces DO NOT need to be quoted, and MUST NOT be quoted, because that is an error. the whole issue here is that the path contained unicode characters, which OpenCV can't handle properly. – Christoph Rackwitz Aug 06 '22 at 23:15

0 Answers0