简体   繁体   中英

base64 to image, corrupt image

I have this code:

with open("bla.png", "wb") as fh:
    fh.write(image.decode('base64'))

and this base64: https://pastebin.com/MKjEdmub The base64 code comes from a canvas from the front-end. I've verified that the string is exactly the same frontend and backend.

If I try to run the code the current base64 code it says: Error: Incorrect padding . I did a bit of research and it turns out that it needs a certain amount of bytes to be converted correctly, hence why it needs = at the end of the string. In this case it needs == at the end of the string. When I do that it converts fine, but the image is corrupt (unreadable).

When I use an online convertor, the current base64 works fine. When I add == it doesn't work in those as well.

I also tried with the decodestring function from the base64 library, but that gave the same error.

Using str.decode('base64') on your data works fine for me, did you remove the data:image/png;base64, header at the beginning? If I don't remove it I also get a padding error. The online tools that you've tried might check for this common syntax and remove it automatically before decoding.

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