如何让OpenCV的imread和imwrite函数支持中文文件名和路径的图像读取与写入?
- 内容介绍
- 文章标签
- 相关推荐
本文共计386个文字,预计阅读时间需要2分钟。
环境:Windows 10、Python 3.6、OpenCV 3.3问题:OpenCV函数cv2.imread()、cv2.imwrite()在读取包含中文路径及命名时出现问题
环境Windows10、Python3.6、OpenCV3.3问题OpenCV函数cv2.imread()、cv2.imwrite()在读取含有中文路径及以中文命名环境Windows10、Python3.6、OpenCV3.3 问题 OpenCV 函数cv2.imread()、cv2.imwrite()在读取含有中文路径及以中文命名的文件时会报错主要原因是因为cv2.imread()、cv2.imwrite()不支持中文。
代码及出错代码
import cv2img cv2.imread(r"G:\Python_work\图片\vikings.jpg")cv2.imshow("img",img)cv2.waitKey()cv2.destroyAllWindows()
报错 OpenCV(3.4.1) Error: Assertion failed (size.width>0
OpenCV cv2.imdecode()、cv2.imencode()方法可以解决。
import cv2import numpy as npimg_path r"G:\Python_work\图片\vikings.jpg"#img cv2.imread(r"G:\Python_work\图片\vikings.jpg")img cv2.imdecode(np.fromfile(img_path,dtypenp.uint8),cv2.IMREAD_UNCHANGED)#也可以写成cv2.imdecode(np.fromfile(img_path,dtypenp.uint8),-1)# cv2.IMREAD_UNCHANGED参数可以用-1代替#cv2.IMREAD_GRAYSCALE:以灰度模式读入图像其值为0#cv2.IMREAD_COLOR:读入彩色图像其值为1#np.fromfile()函数相对应的函数为np.tofile()img_write cv2.imencode(".jpg",img)[1].tofile(img_path)#cv2.imencode()函数返回两个值;写入成功返回Ture另一个值为数组.#_,im_encode cv2.imencode(".jpg",img)cv2.imshow("img",img)cv2.waitKey()cv2.destroyAllWindows()
其中 cv2.imwrite() 的解决方法为
cv2.imwrite(imagepath, frame)
修改为
cv2.imencode(.jpg, frame)[1].tofile(imagepath)
参考链接
blog.csdn.net/kebu12345678/article/details/54837245NumPy 文件存取 tofilefromfile loadsave;
blog.csdn.net/dcrmg/article/details/79155233(OpenCV-Python cv2.imdecode()和cv2.imencode() 图片解码和编码)
本文共计386个文字,预计阅读时间需要2分钟。
环境:Windows 10、Python 3.6、OpenCV 3.3问题:OpenCV函数cv2.imread()、cv2.imwrite()在读取包含中文路径及命名时出现问题
环境Windows10、Python3.6、OpenCV3.3问题OpenCV函数cv2.imread()、cv2.imwrite()在读取含有中文路径及以中文命名环境Windows10、Python3.6、OpenCV3.3 问题 OpenCV 函数cv2.imread()、cv2.imwrite()在读取含有中文路径及以中文命名的文件时会报错主要原因是因为cv2.imread()、cv2.imwrite()不支持中文。
代码及出错代码
import cv2img cv2.imread(r"G:\Python_work\图片\vikings.jpg")cv2.imshow("img",img)cv2.waitKey()cv2.destroyAllWindows()
报错 OpenCV(3.4.1) Error: Assertion failed (size.width>0
OpenCV cv2.imdecode()、cv2.imencode()方法可以解决。
import cv2import numpy as npimg_path r"G:\Python_work\图片\vikings.jpg"#img cv2.imread(r"G:\Python_work\图片\vikings.jpg")img cv2.imdecode(np.fromfile(img_path,dtypenp.uint8),cv2.IMREAD_UNCHANGED)#也可以写成cv2.imdecode(np.fromfile(img_path,dtypenp.uint8),-1)# cv2.IMREAD_UNCHANGED参数可以用-1代替#cv2.IMREAD_GRAYSCALE:以灰度模式读入图像其值为0#cv2.IMREAD_COLOR:读入彩色图像其值为1#np.fromfile()函数相对应的函数为np.tofile()img_write cv2.imencode(".jpg",img)[1].tofile(img_path)#cv2.imencode()函数返回两个值;写入成功返回Ture另一个值为数组.#_,im_encode cv2.imencode(".jpg",img)cv2.imshow("img",img)cv2.waitKey()cv2.destroyAllWindows()
其中 cv2.imwrite() 的解决方法为
cv2.imwrite(imagepath, frame)
修改为
cv2.imencode(.jpg, frame)[1].tofile(imagepath)
参考链接
blog.csdn.net/kebu12345678/article/details/54837245NumPy 文件存取 tofilefromfile loadsave;
blog.csdn.net/dcrmg/article/details/79155233(OpenCV-Python cv2.imdecode()和cv2.imencode() 图片解码和编码)

