1

Hello I'm new to the concept of base64 images. I was trying to convert base64 "links" in a HTML to png files in Python, but the png generated seems to be damaged and I don't know why... Here is my code (in python 3.6)

encoded = (string2[0].split(",")[1]).encode("utf-8")
with open(r"myDirectory\example1.png", "wb") as fh:
    fh.write(base64.decodebytes(encoded))

string2[0] is the full base64 string which I copied from the HTML. i.e. something like

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA0gAA...K5C%0AYII=

The problem is essentially the following: A png file will be generated, but when I open it, windows says "the file appears to be damaged, corrupted". However strangely when I open this base64 string in google chrome, the photo can be shown.

Anyone has encountered similar situation before?

p.s. I was thinking to provide the full base64 string, but it's very very long. Anyone knows how to paste such a long string to the question? e.g. a "dragable box of code" similar to what the OP has done in this question

Edit: The base64 string can be found here. My first time sharing documents in google drive - let me know if you guys can access it.

Min
  • 179
  • 9
  • While posting, you may edit the base64 string as `code` and you will get the "dragable box of code". – amanb Apr 18 '18 at 18:13
  • @user8212173 Thanks! But the string is so long that it has already exceeds the character limit on posting a question... So can't really do that. Thanks anyway! – Min Apr 18 '18 at 19:22
  • Create a smaller image and post that one's base64 code. You can also try to upload one of the damaged PNGs on a public server and provide a link (I don't think you can attached a 'damaged' file to an SO post). – Jongware Apr 18 '18 at 22:14
  • 1
    @usr2564301 Good idea thanks! I've just added a link to the google drive. Let me know if you can access it. – Min Apr 18 '18 at 22:24

0 Answers0