1

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.

4
  • Would you be able to include the base64-encoded image, or a link to it, so that we can give it a try? Commented Mar 4, 2021 at 19:21
  • Thanks for your response.Can you get it from the onedrive link above ? 1drv.ms/t/s!AkTtiXv5QMtWliMbeziGpd0t1EjW?e=JDHEkt Commented Mar 4, 2021 at 19:35
  • What makes you think that all the things you've flagged as "bad characters" are really bad? Many of them look like artifacts of printing a string from Python, for example the leading b'. There's a 1/65536 chance of any 2-character sequence in a random bunch of bytes to match that. Commented Mar 4, 2021 at 20:04
  • I just tested it with and without and it seemed not to work with an initial b'. But this might be wrong because of my initial misstake. Commented Mar 4, 2021 at 20:58

1 Answer 1

1

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 +"==="
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. I missed one \. at the first filterig, because I thought replace('\n') would do the job . This took me hours. Hope this helps someody in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.