Reputation: 1221
I'm using Python Flask as my backend and faced a little problem. In the frontend application I have a form that contains an image upload feature.
In the backend I refer a variable to the image with
image = request.files['image']
That exports a FileStorage
object.
I want to convert the image to base64 format in order to insert it to my DB. I tried a lot of things but nothing worked. Anyone knows?
Upvotes: 12
Views: 20779
Reputation: 1802
Basically you need to read it as a stream and then convert it to base64 format. Check the following answer:
Encoding an image file with base64
The solution shoud look like this:
import base64
...
image = request.files['image']
image_string = base64.b64encode(image.read())
Upvotes: 21