I have got the following Base64 representation of an image I get send to my redis server from an application: https://1drv.ms/t/s!AkTtiXv5QMtWliMbeziGpd0t1EjW?e=JDHEkt
Here's an excerpt of the data for those who don't want to download the whole 13MB data file:
b'\\/9j\\/4b6URXhpZgAASUkqAAgAAAAMAAABBAABAAAAoA8AAAEBBAABAAAAuAsAAA8BAgAIAAAAngAA\\nABABAgAJAAAApgAAABIBAwABAAAAAQAAABoBBQABAAAA0gAAABsBBQABAAAA2gAAACgBAwABAAAA\\nAgAAADEBAgAOAAAAsAAAADIBAgAUAAAAvgAAABMCAwABAAAAAQAAAGmHBAABAAAA4gAAAIQCAABz\\nYW1
I tried to repaire the b64 with the following :
import base64
with open('Outputfirst.txt', 'r') as file:
imgstring = file.read().replace('\n', '')
#get rid of wrong characters
imgstring = imgstring.replace("b'",'')
imgstring = imgstring.replace('\\','')
imgstring = imgstring.replace('"','')
imgstring = imgstring.replace('\'','')
imgstring = imgstring.replace(',','')
#take care of padding
if(len(imgstring)%4 ==1):
imgstring = imgstring +"==="
if(len(imgstring)%4 ==2):
imgstring = imgstring +"=="
if(len(imgstring)%4 ==3):
imgstring = imgstring +"="
imgstring = bytes(imgstring,'utf8')
with open(filename, 'wb') as f:
f.write(imgstring)
imgdata = base64.b64decode(imgstring)
filename = 'some_image3.jpg'
with open(filename, 'wb') as f:
f.write(imgdata)
But somehow I dont get the image back properly.
When I use this tool though https://base64.guru/tools/repair and take its output as input for my script, I get the image I want.
It seems \\n
doesn't get filtered out.
The whole filtering and padding can be done like so:
with open('Outputfirst.txt', 'r') as file:
imgstring = file.read().replace('\\n', '').replace('\\','').replace("b'",'')
imgstring = imgstring + '=' * (4 - len(imgstring) % 4)
Padding with 3 '=' is invalid:
if(len(imgstring)%4 ==1):
imgstring = imgstring +"==="