Categories
Python Answers

How to decode base64 from POST to use in PIL and Python?

Spread the love

Sometimes, we want to decode base64 from POST to use in PIL and Python.

In this article, we’ll look at how to decode base64 from POST to use in PIL and Python.

How to decode base64 from POST to use in PIL and Python?

To decode base64 from POST to use in PIL and Python, we can call Image.open to open the image.

For instance, we write

from PIL import Image
from io import BytesIO
import base64

data['img'] = '''iVBORw0KGgoAAAANSUhEUgAAAAUA
    AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
        9TXL0Y4OHwAAAABJRU5ErkJggg=''' 

im = Image.open(BytesIO(base64.b64decode(data['img'])))

to call base64.b64decode to decode the base64 string into bytes.

Then we use the bytes to create the BytesIO object.

And then we can open the bytes as an image with Image.open.

Conclusion

To decode base64 from POST to use in PIL and Python, we can call Image.open to open the image.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *