How to convert string in base64 to image and save on filesystem with Python?

Sometimes, we want to convert string in base64 to image and save on filesystem with Python.

In this article, we’ll look at how to convert string in base64 to image and save on filesystem with Python.

How to convert string in base64 to image and save on filesystem with Python?

To convert string in base64 to image and save on filesystem with Python, we can call the base64.decodebytes method.

For instance, we write

import base64

with open("imageToSave.png", "wb") as fh:
    fh.write(base64.decodebytes(img_data))

to call open to open imageToSave.png with write permission as a binary file.

Then we call write with the decoded base64 image data that we get from

base64.decodebytes(img_data)

to save the image as imageToSave.png.

img_data is a base64 string with the image data.

Conclusion

To convert string in base64 to image and save on filesystem with Python, we can call the base64.decodebytes method.