Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Python图像处理】遇到过的问题闲记 #28

Open
jayboxyz opened this issue Dec 19, 2019 · 1 comment
Open

【Python图像处理】遇到过的问题闲记 #28

jayboxyz opened this issue Dec 19, 2019 · 1 comment
Labels
Python&常用库 Python、Python常用库等

Comments

@jayboxyz
Copy link
Owner

No description provided.

@jayboxyz jayboxyz added the Python&常用库 Python、Python常用库等 label Dec 19, 2019
@jayboxyz
Copy link
Owner Author

使用 Python3 + OpenCV 时,遇到读取/写出中文路径的图片的问题

解决方法:

1、读取 imread

def cv_imread(file_path):
    cv_img = cv2.imdecode(np.fromfile(file_path, dtype=np.uint8), -1)
    return cv_img

通过numpy读取文件,再编码为cv2的图片对象,避免了OpenCV不支持中文路径的问题。调用时,替换cv2.imread即可:

# img = cv2.imread(curimg_path)
img = cv_imread(curimg_path)

2、保存 imwrite

同样地,图片输出到含有中文的路径也需要使用一个小函数:

def cv_imwrite(img, path):
    suffix = os.path.splitext(path)[-1]
    cv2.imencode(suffix, img)[1].tofile(path)

调用时,替换cv2.imwrite即可:

# cv2.imwrite(path, img)
cv2_imwrite(img, path)

参考:https://heary.cn/posts/OpenCV-%E4%B8%AD%E6%96%87%E8%B7%AF%E5%BE%84%E9%97%AE%E9%A2%98/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Python&常用库 Python、Python常用库等
Projects
None yet
Development

No branches or pull requests

1 participant