1

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.

asked Nov 27, 2019 at 8:19
1

1 Answer 1

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.