xer*_*ool 29 python binary opencv image
我正在使用PyOpenCV.如何将cv2映像(numpy)转换为二进制字符串,以便在没有临时文件的情况下写入MySQL数据库imwrite
?
我用谷歌搜索但没有发现......
我正在尝试imencode
,但它不起作用.
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()))
Run Code Online (Sandbox Code Playgroud)
错误:
File "server.py", line 16, in do_GET
self.wfile.write(cv2.imencode('png', capture.read()))
TypeError: img is not a numerical tuple
Run Code Online (Sandbox Code Playgroud)
帮助别人!
jab*_*edo 63
如果您有一个图像img
(这是一个numpy数组),您可以使用以下方法将其转换为字符串:
>>> img_str = cv2.imencode('.jpg', img)[1].tostring()
>>> type(img_str)
'str'
Run Code Online (Sandbox Code Playgroud)
现在,您可以轻松地将图像存储在数据库中,然后使用以下命令恢复它:
>>> nparr = np.fromstring(STRING_FROM_DATABASE, np.uint8)
>>> img = cv2.imdecode(nparr, cv2.CV_LOAD_IMAGE_COLOR)
Run Code Online (Sandbox Code Playgroud)
您需要将STRING_FROM_DATABASE
包含查询结果的变量替换为包含该图像的数据库.
这是一个例子:
def image_to_bts(frame):
'''
:param frame: WxHx3 ndarray
'''
_, bts = cv2.imencode('.webp', frame)
bts = bts.tostring()
return bts
def bts_to_img(bts):
'''
:param bts: results from image_to_bts
'''
buff = np.fromstring(bts, np.uint8)
buff = buff.reshape(1, -1)
img = cv2.imdecode(buff, cv2.IMREAD_COLOR)
return img
Run Code Online (Sandbox Code Playgroud)
小智 7
它在 2020 年使用 numpy==1.19.4 和 opencv==4.4.0:
import cv2
cam = cv2.VideoCapture(0)
# get image from web camera
ret, frame = cam.read()
# convert to jpeg and save in variable
image_bytes = cv2.imencode('.jpg', frame)[1].tobytes()
Run Code Online (Sandbox Code Playgroud)
capture.read() 返回一个元组,(err,img)。
尝试拆分它:
_,img = capture.read()
self.wfile.write(cv2.imencode('png', img))
Run Code Online (Sandbox Code Playgroud)
小智 5
im = cv2.imread('/tmp/sourcepic.jpeg')
res, im_png = cv2.imencode('.png', im)
with open('/tmp/pic.png', 'wb') as f:
f.write(im_png.tobytes())
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
56649 次 |
最近记录: |