opencv-python读取/保存带中文路径的图片

2021-01-19  本文已影响0人  1037号森林里一段干木头

简介:opencv-python不支持用cv.imread和cv.imwrite处理带中文路径的图片,但是可以采用从内存编解码的方式获取图像,在此造个轮子方便以后使用

1. opencv函数原型

- 读取带中文路径的图片

imdecode(buf, flags) -> retval.
. @brief Reads an image from a buffer in memory.
. . The function imdecode reads an image from the specified buffer in the memory. If the buffer is too short or . contains invalid data, the function returns an empty matrix ( Mat::data==NULL ).
. . See cv::imread for the list of supported formats and flags description.
. . @note In the case of color images, the decoded images will have the channels stored in B G R order.
. @param buf Input array or vector of bytes.
. @param flags The same flags as in cv::imread, see cv::ImreadModes.
Type: builtin_function_or_method

- 保存带中文路径的图片

2. 实例:

import cv2 as cv
import numpy as np

path = r"H:\\需要生成fov定位点的图\\fov\\LUQIAN\\1\\0-28.bmp"
img = cv.imdecode(np.fromfile(path, dtype=np.uint8),1)
cv.namedWindow("img",0)
cv.imshow("img",img)
cv.waitKey(0)
cv.destroyWindow("img")

outpath = u"./图片1.jpg"
cv.imencode('.jpg',img)[1].tofile(outpath) #".jpg"是编码方式,可以改成“.bmp” ".png".....
上一篇 下一篇

猜你喜欢

热点阅读