opencv保存图片路径包含中文乱码解决方案

# coding: utf-8
import numpy as np
import cv2

img = cv2.imread('1.jpg',1)
cv2.imshow('image', img)

k = cv2.waitKey(0)

if k == 27:
   cv2.destroyAllWindows() 
elif k == ord('s'):
    cv2.imwrite('保存_1.jpg', img)   
    cv2.destroyAllWindows() 

按S后图片保存成功,中文乱码如下图:


解决方案:

将cv2.imwrite('保存_1.jpg', img)替换为cv2.imencode('.jpg', img)[1].tofile(filenpath)

完整代码如下:

# coding: utf-8
import numpy as np
import cv2

img = cv2.imread('1.jpg',1)
cv2.imshow('image', img)

k = cv2.waitKey(0)

if k == 27:
   cv2.destroyAllWindows() 
elif k == ord('s'):
    #cv2.imwrite('保存_1.jpg', img)   
    cv2.imencode('.jpg', img)[1].tofile('保存_1.jpg')
    cv2.destroyAllWindows() 

效果:



posted on 2018-07-03 13:41  疯狂的小萝卜头  阅读(2090)  评论(0编辑  收藏  举报