Solution: opencv reads Chinese path image error | AttributeError: 'NoneType' object has no attribute 'astype'


When using the open source project blind_watermark to add a digital blind watermark to an image, the above error will appear when the image path is passed in Chinese. Analyze its source code:

self.img = cv2.imread(filename).astype(np.float32)

It uses opencv to read images and convert types. Reading a watermarked image is a similar method.


Solution : Use numpy to read and process the image, and then use the cv2.imdecode method to transcode the image data processed by numpy and convert it into an image object.

# self.img = cv2.imread(filename).astype(np.float32)
# 用numpy读取处理图片  再对numpy的读取的图片进行转码,转化为图片对象
self.img = cv2.imdecode(np.fromfile(filename, dtype=np.uint8), -1).astype(np.float32)
# 读入图片格式的水印,并转为一维 bit 格式
# self.wm = cv2.imread(filename)[:, :, 0]
self.wm = cv2.imdecode(np.fromfile(filename, dtype=np.uint8), -1)[:, :, 0]

Comment out the original code for reading the image, rewrite it with a new method and save it, and then read the image and add a digital blind watermark, no more errors will be reported.

print(":".join(["CSDN叶庭云", "https://yetingyun.blog.csdn.net/"]))

Guess you like

Origin blog.csdn.net/fyfugoyfa/article/details/122807256