简体   繁体   中英

How to Decode base64 Image URIs

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM