12

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?

GMe
  • 1,091
  • 3
  • 13
  • 24

1 Answers1

18

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())
Avi K.
  • 1,734
  • 2
  • 18
  • 28