How to read an image from a path with Unicode characters?

I have the following code and it does not work because it cannot read the file from disk. The image is always None.

# -*- coding: utf-8 -*-
import cv2
import numpy

bgrImage = cv2.imread(u'D:\\ö\\handschuh.jpg')

Note: my file is already saved as UTF-8 with specification. I have been tested with Notepad ++.

In Process Monitor, I see that Python is accessing the file in the wrong way:

Process monitor

I read about:

+11
source share
3 answers

This can be done using

  • open(), Unicode, ,
  • ,
  • NumPy,
# -*- coding: utf-8 -*-
import cv2
import numpy

stream = open(u'D:\\ö\\handschuh.jpg', "rb")
bytes = bytearray(stream.read())
numpyarray = numpy.asarray(bytes, dtype=numpy.uint8)
bgrImage = cv2.imdecode(numpyarray, cv2.IMREAD_UNCHANGED)
+23

, np.fromfile(), ndarray, cv2.imdecode() ndarray (, -):

import numpy as np

# img is in BGR format if the underlying image is a color image
img = cv2.imdecode(np.fromfile('测试目录/test.jpg', dtype=np.uint8), cv2.IMREAD_UNCHANGED)

np.fromfile() 1- . cv2.imdecode . cv2.IMREAD_UNCHANGED . .

PS. , . .

0
bgrImage = cv2.imread(filename.encode('utf-8'))

utf-8

-4

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


All Articles