2

Can someone help me turn this base64 data into image? I don't know if it's because the data was not decoded properly or anything else. Here is how I decoded the data:

import base64

c_data = { the data in the link (string type) }

c_decoded = base64.b64decode(c_data)

But it gave the error Incorrect Padding so I followed some tutorials and tried different ways to decode the data.

c_decoded = base64.b64decode(c_data + '=' * (-len(c_data) % 4))

c_decoded = base64.b64decode(c_data + '=' * ((4 - len(c_data) % 4) % 4)

Both ways decoded the data without giving the error Incorrect Padding but now I can't turn the decoded data into image.
I have tried creating an empty png then write the decoded data into it:

from PIL import Image

with open('c.png', 'wb') as f:
    f.write(c_decoded)
image = Image.open('c.png')
image.show()

It didn't work and gave the error: cannot identify image file 'c.png'
I have tried using BytesIO:

from PIL import Image
import io
from io import BytesIO

image = Image.open(io.BytesIO(c_decoded))
image.show()

Now it gave the error: cannot identify image file <_io.BytesIO object at 0x0000024082B20270>
Please help me.

gay
  • 57
  • 5
  • Can you try uploading your Base64 encoding to this online decoder to see if it actually is a proper Base64 PNG? https://www.opinionatedgeek.com/codecs/base64decoder Download the output as `something.png` and try opening it on your laptop. – Eric Wang Oct 02 '22 at 02:59
  • @EricWang the base64 is converted into a corrupted text with a lot of werid characters on your website. But I have tried several converters online [link](https://base64-to-image.com/) [link](https://codebeautify.org/base64-to-image-converter) [link](https://base64.guru/converter/decode/image) [link](https://www.rapidtables.com/web/tools/base64-to-image.html) and they work just fine – gay Oct 02 '22 at 03:28
  • 1
    Did you take the complete string that you show in your link? Then that's wrong, that's not a base64 encoded image but a DataURI which contains a Base64 image. The first part up to `,` is the DataURI part and needs to be deleted from the string before you convert it. The base 64 string starts after the `,` with `iVBORw...`. – jps Oct 02 '22 at 07:32
  • @jps Exactly... – Mark Setchell Oct 02 '22 at 07:44

2 Answers2

2

Not sure if you definitely need a Python solution, or you just want help decoding your image like the first line of your question says, and you thought Python might be needed.

If the latter, you can just use ImageMagick in the Terminal:

cat YOURFILE.TXT | magick inline:- result.png

enter image description here

Or equivalently and avoiding "Useless Use of cat":

magick inline:- result.png < YOURFILE.TXT

If the former, you can use something like this (untested):

from urllib import request

with request.urlopen('data:image/png;base64,iVBORw0...')  as response:
   im = response.read()

Now im contains a PNG-encoded [^1] image, so you can either save to disk as such:

with open('result.png','wb') as f:
    f.write(im)

Or, you can wrap it in a BytesIO and open into a PIL Image:

from io import BytesIO
from PIL import Image

pilImage = Image.open(BytesIO(im))

[^1]: Note that I have blindly assumed it is a PNG, it may be JPEG, so you should ideally look at the start of the DataURI to determine a suitable extension for saving your file.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • I am writing a python to srape data from a website and there is a list of images like this so I want a small function in my code to decode all the image data. Someone pointed out why I couldn't get the image. I decoded it wrong tho. But thanks still for suggesting this answer, I will try it later when I finish my code. – gay Oct 02 '22 at 09:09
  • The `urllib` code in my answer is doing exactly what @jps suggested for you - i.e. stripping off the `data:image/png;base64,` in a reliable way and then base64-decoding for you as well. – Mark Setchell Oct 02 '22 at 09:14
  • Oh ok. I will mark it as a solution. Thanks a lot! – gay Oct 02 '22 at 09:17
2

Credit to @jps for explaining why my code didn't work. Check out @Mark Setchell solution for the reliable way of decoding base64 data (his code fixes my mistake that @jps pointed out)

So basically remove the [data:image/png;base64,] at the beginning of the base64 string because it is just the format of the data. Change this:

c = "data:image/png;base64,iVBORw0KGgoAAAANSUh..."

to this:

c = "iVBORw0KGgoAAAANSUh..."

and now we can use

c_decoded = base64.b64decode(c)
gay
  • 57
  • 5