Python的OpenCV的图像转换为字节字符串? [英] Python OpenCV convert image to byte string?

查看:5695
本文介绍了Python的OpenCV的图像转换为字节字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与PyOpenCV工作。如何CV2图像(numpy的)转换为二进制字符串没有一个临时文件写入到MySQL数据库和 imwrite

I'm working with PyOpenCV. How to convert cv2 image (numpy) to binary string for writing to MySQL db without a temporary file and imwrite?

我是谷歌,但没有发现...

I'm google it but nothing found...

我试图 imen code ,但它并不作品

capture = cv2.VideoCapture(url.path)
capture.set(cv2.cv.CV_CAP_PROP_POS_MSEC, float(url.query))
self.wfile.write(cv2.imencode('png', capture.read()))

错误:

  File "server.py", line 16, in do_GET
  self.wfile.write(cv2.imencode('png', capture.read()))
  TypeError: img is not a numerical tuple

帮助别人!

推荐答案

如果你有一个形象 IMG (这是一个numpy的数组),你可以用它转换成字符串

If you have an image img (which is a numpy array) you can convert it into string using:

>>> img_str = cv2.imencode('.jpg', img)[1].tostring()
>>> type(img_str)
 'str'

没有您可以轻松地将影像存储数据库里面,然后通过恢复它:

No you can easily store the image inside your database, and then recover it by using:

>>> nparr = np.fromstring(STRING_FROM_DATABASE, np.uint8)
>>> img = cv2.imdecode(nparr, cv2.CV_LOAD_IMAGE_COLOR)

在您需要使用包含您的查询包含图像数据库的结果变量来代替 STRING_FROM_DATABASE

这篇关于Python的OpenCV的图像转换为字节字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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