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

简介: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

- 保存带中文路径的图片

  • imencode(ext, img[, params]) -> retval, buf.
    . @brief Encodes an image into a memory buffer.
    . . The function imencode compresses the image and stores it in the memory buffer that is resized to fit the . result. See cv::imwrite for the list of supported formats and flags description.
    . . @param ext File extension that defines the output format.
    . @param img Image to be written.
    . @param buf Output buffer resized to fit the compressed image.
    . @param params Format-specific parameters. See cv::imwrite and cv::ImwriteFlags.
    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".....
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容