如何在Python3中使用opencv从文件缓冲区读取文件 [英] How to use opencv in Python3 to read file from file buffer
本文介绍了如何在Python3中使用opencv从文件缓冲区读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用此库.特别是以下3行:
I am trying to use this library. In particular these 3 lines:
image_stream = io.BytesIO(image_bytes)
frame = cv2.imread(image_stream)
我有一个例外:
Traceback (most recent call last):
File "/home/a/Pictures/pycharm-community-2018.3.2/helpers/pydev/pydevd.py", line 1741, in <module>
main()
File "/home/a/Pictures/pycharm-community-2018.3.2/helpers/pydev/pydevd.py", line 1735, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "/home/a/Pictures/pycharm-community-2018.3.2/helpers/pydev/pydevd.py", line 1135, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/home/a/Pictures/pycharm-community-2018.3.2/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/home/a/Documents/wiker/main.py", line 13, in <module>
if __name__ == '__main__': main()
File "/home/a/Documents/wiker/main.py", line 10, in main
article['video'] = video.make_video_from_article(article)
File "/home/a/Documents/wiker/video.py", line 15, in make_video_from_article
frame = cv2.imread(image_stream)
TypeError: bad argument type for built-in operation
但是它适用于真实文件.可以在这里解决什么.
But it works on real files. what can be fixed here.
推荐答案
以下是解决方法:
import io, requests, cv2, numpy as np
url = "https://images.pexels.com/photos/236047/pexels-photo-236047.jpeg"
img_stream = io.BytesIO(requests.get(url).content)
img = cv2.imdecode(np.fromstring(img_stream.read(), np.uint8), 1)
cv2.imshow("img", img)
cv2.waitKey(0)
这篇关于如何在Python3中使用opencv从文件缓冲区读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文