1

I'm trying to load an image from its absolute path, but neither cv2.imread nor cv2.cv.LoadImage are working.

When I use the relative path, it works perfectly.

I've already tried different ways:

import cv2
 
#works perfectly
print(cv2.imread('larvae.png'))   

#don't work
print(cv2.imread('C:/Users/moniq/Documentos/Automação - Contagem de Larvas/larvae.png'))
#output: None

#don't work
print(cv2.imread('C:\Users\moniq\Documentos\Automação - Contagem de Larvas\larvae.png'))
#output: SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

#don't work
print(cv2.imread('C:\\Users\\moniq\\Documentos\\Automação - Contagem de Larvas\\larvae.png'))
#output: None
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    Try to omit special characters like in çã or make sure that the underlying strings are able to interpret them correctly. Your first and your third version should work in general, if you have read access to that path. In the second version try to add an r beforen the first ' – Micka Aug 09 '21 at 17:40
  • @Micka how can I handle with the special characters? – Monique Schreiner Aug 09 '21 at 18:08
  • try this one: https://stackoverflow.com/a/10180956/2393191 feed the result (encoding) to opencv imread – Micka Aug 09 '21 at 18:39

1 Answers1

1

I finally found the answer here: https://stackoverflow.com/a/60749818/12119896]

cv2.imread() can silently fail when the path is too long, that is why it worked when I use the relative path.

The solution was to change the read function. I using the one from matplotlib now:

import matplotlib.pyplot as plt    

image = plt.imread('C:/Users/moniq/Documentos/Automação - Contagem de Larvas/larvae.png')