由于文件路径中的特殊字符,OpenCv imwrite无法正常工作 [英] OpenCv imwrite doesn't work because of special character in file path

查看:328
本文介绍了由于文件路径中的特殊字符,OpenCv imwrite无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当文件路径具有特殊字符(例如é")时,我无法保存图像.

I can't save an image when the file path has special character (like "é" for example).

这是来自Python 3 shell的测试:

Here is a test from Python 3 shell :

>>> cv2.imwrite('gel/test.jpg', frame)
True
>>> cv2.imwrite('gel/ééé/test.jpg', frame)
False
>>> cv2.imwrite('gel/eee/test.jpg', frame)
True

有什么想法怎么做?

谢谢!

不幸的是,@ PM2Ring和@DamianLattenero提出的所有建议似乎都无效:(

Unfortunately, all suggestions proposed by @PM2Ring and @DamianLattenero don't seem to work :(

所以,我使用@cdarke的解决方案,这是我的最终代码:

So, I use the @cdarke's solution, here's my final code :

destination = 'gel/ééé/'
gel = 'test.jpg'
script_path = os.getcwd()
os.chdir(destination)
cv2.imwrite(gel, frame)
os.chdir(script_path)

推荐答案

您可以先使用OpenCV对图像进行编码,然后使用numpy tofile()方法保存,因为编码后的图像是一维numpy ndarray:

You can first encode the image with OpenCV and then save it with numpy tofile() method since the encoded image is a one-dim numpy ndarray:

is_success, im_buf_arr = cv2.imencode(".jpg", frame)

im_buf_arr.tofile('gel/ééé/test.jpg')

这篇关于由于文件路径中的特殊字符,OpenCv imwrite无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆