解决opencv读取和存储带有中文图片的方法

  • Post author:
  • Post category:其他


opencv一般情况下读取图片的代码为

cv2.imread(xxx.jpg)

写入图片的代码为

cv2.imwrite('/xxxx/xxx.jpg', out_img)#前面是图片路径,后面是图片名

但是如果遇到图片名是中文的话,使用以上代码会报错,需要将以上代码进行更改!!!!

读取图片代码改为

cv2.imdecode(np.fromfile(file_path,dtype=np.uint8),-1)    #读取

写入图片的代码改为

cv2.imencode('.jpg', img)[1].tofile(file_path)

或者

cv2.imencode('.jpg', img).tofile(file_path)

PS:imencode后面的[1]取决于你的元组有多少,如果报错

AttributeError: tuple’ object has no attribute ‘tofile’

,就说明需要加[1]



版权声明:本文为yeahxbf原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。