I want to convert frame from a video file to base64 without save in directory.
I use: img = frame.copy()
to take a frame from my video and it return a numpy array.
How can i convert it into base64 to store in database and display it into web.
Thanks for your help.
1 Answer 1
The following code should work - note you'll have to switch around the format as needed, and the corresponding base64 string. In this example I've used 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...`
answered Nov 27, 2019 at 10:53
Comments
lang-py
base64
library docs.python.org/3/library/base64.html