1

I'm connected to an API and one of the fields that it returns is this:

'image':'data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='

I know this is an base64 encoding of an image but I'm not sure how to decode it, I have already tested many online tools (just for checking the format it works) but none of them gave me the final image, how can I decode this with Python?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Stack
  • 1,028
  • 2
  • 10
  • 31

1 Answers1

0

This works in Python3. The stuff before the string is just meta information that you need to skip over.

data='data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
img_data=data.split('base64,')[1].encode('utf8')
with open("imageToSave.gif", "wb") as fh:
    fh.write(base64.decodebytes(img_data))

The image appears to be a 1 pixel GIF. Presumably a tracker...

Source: Convert string in base64 to image and save on filesystem in Python

forgetso
  • 2,194
  • 14
  • 33