[英]Convert numpy array to base64
I want to convert frame from a video file to base64 without save in directory.我想将帧从视频文件转换为 base64 而不保存在目录中。
I use: img = frame.copy()
to take a frame from my video and it return a numpy array.我使用:
img = frame.copy()
从我的视频中获取一帧并返回一个 numpy 数组。
How can i convert it into base64 to store in database and display it into web.如何将其转换为 base64 以存储在数据库中并将其显示到 web.xml 中。
Thanks for your help.谢谢你的帮助。
The following code should work - note you'll have to switch around the format as needed, and the corresponding base64 string.以下代码应该可以工作 - 请注意,您必须根据需要切换格式和相应的 base64 字符串。 In this example I've used PNG.
在这个例子中,我使用了 PNG。
from PIL import Image
from io import BytesIO
import base64
data = frame.copy()
image_out = Image.fromarray(data)
buffer = BytesIO()
image_out.save(buffer, format="PNG")
base64_str = "data:image/png;base64," + base64.b64encode(buffer.getvalue()).decode("utf-8")
Output:输出:
>>> base64_str
`data:image/png;base64,iVBORw...`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.