
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...
I'm trying imencode, but it doesn't works
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()))
Error:
File "server.py", line 16, in do_GET
self.wfile.write(cv2.imencode('png', capture.read()))
TypeError: img is not a numerical tuple
Help somebody!
解决方案
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)
where you need to replace STRING_FROM_DATABASE with the variable that contains the result of your query to the database containing the image.
本文介绍如何将PyOpenCV中的图像数据转换为二进制字符串,以便于直接存储到MySQL数据库中,避免使用临时文件和imwrite方法。通过使用cv2.imencode方法并结合numpy操作实现这一目标。
3495

被折叠的 条评论
为什么被折叠?



