When you open outfile in base64_decode_image, you do not specify the ofstream::binary flag like you do in base64_encode_image when reading the image. Without that flag, you're writing in text mode which can alter the data you're writing (when adjusting for newlines).
Decoding and saving image files from base64 C++ - Stack Overflow
url - Decoding Base64 Images - Stack Overflow
decoding base64 image data
python - Cannot convert base64 string into image - Stack Overflow
What Is A Base64 Image Decoder?
What File Types Can Be Decoded From Base64?
Is Base64 Decoding Safe To Use Online?
When you open outfile in base64_decode_image, you do not specify the ofstream::binary flag like you do in base64_encode_image when reading the image. Without that flag, you're writing in text mode which can alter the data you're writing (when adjusting for newlines).
In the base64_decode method in the is_base64 check, the β=β is not taken into account and the method terminates with an error.
Use this web utility:
http://www.motobit.com/util/base64-decoder-encoder.asp
Set the output format to binary, then copy-paste the base64 data that follows data:image/png;base64,; your browser will download the file. Rename it to PNG and you're good to go.
Another way to save the image is just to copy-paste data:image/png;base64, followed by the characters in your browser's url field..
and you'll see the image..
you could then save it to your computer..
Im having trouble decoding some base64 encoded data. I generated the data using 'toDataURL' on a canvas element. I then pass this to some python code using flask.
Im trying to use the 'base64' module but i keep getting the error -
Invalid base64-encoded string: number of data characters (1517) cannot be 1 more than a multiple of 4
This is the python code causing the error -
img_data = request.form["image"]
img_bytes = base64.b64decode(img_data)
img = Image.open(BytesIO(img_bytes))
img = np.array(img)
cv.imshow(img)
cv.waitKey(0)
cv.destroyAllWindows()The line causing the error is -
img_bytes = base64.b64decode(img_data)
The base64 data -
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/4gIoSUNDX1BST0ZJTEUAAQEAAAIYAAAAAAQwAABtbnRyUkdCIFhZWiAAAAAAAAAAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAA9tYAAQAAAADTLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlkZXNjAAAA8AAAAHRyWFlaAAABZAAAABRnWFlaAAABeAAAABRiWFlaAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD//2Q==
Do i pass this whole string to the decoder, or just some of it?
I have also tried the urlSafe decoder that is included with the base64 module. But i get the same error. What am i doing wrong ...
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)
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

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.