Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

save base64 image python

Tags:

python

image

base

I am trying to save an image with python that is Base64 encoded. Here the string is to large to post but here is the image

enter image description here

And when received by python the last 2 characters are == although the string is not formatted so I do this

import base64
data = "data:image/png;base64," + photo_base64.replace(" ", "+")

And then I do this

imgdata = base64.b64decode(data)
    filename = 'some_image.jpg'  # I assume you have a way of picking unique filenames
    with open(filename, 'wb') as f:
            f.write(imgdata)

But this causes this error

Traceback (most recent call last):
  File "/var/www/cgi-bin/save_info.py", line 83, in <module>
    imgdata = base64.b64decode(data)
  File "/usr/lib64/python2.7/base64.py", line 76, in b64decode
    raise TypeError(msg)
TypeError: Incorrect padding

I also printed out the length of the string once the data:image/png;base64, has been added and the spaces replace with + and it has a length of 34354, I have tried a bunch of different images but all of them when I try to open the saved file say that the file is damaged.

What is happening and why is the file corrupt?

Thanks

EDIT

Here is some base64 that also failed

iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADBQTFRFA6b1q Ci5/f2lt/9yu3 Y8v2cMpb1/DSJbz5i9R2NLwfLrWbw m T8I8////////SvMAbAAAABB0Uk5T////////////////////AOAjXRkAAACYSURBVHjaLI8JDgMgCAQ5BVG3//9t0XYTE2Y5BPq0IGpwtxtTP4G5IFNMnmEKuCopPKUN8VTNpEylNgmCxjZa2c1kafpHSvMkX6sWe7PTkwRX1dY7gdyMRHZdZ98CF6NZT2ecMVaL9tmzTtMYcwbP y3XeTgZkF5s1OSHwRzo1fkILgWC5R0X4BHYu7t/136wO71DbvwVYADUkQegpokSjwAAAABJRU5ErkJggg==

This is what I receive in my python script from the POST Request

Note I have not replace the spaces with +'s

like image 836
spen123 Avatar asked Dec 06 '15 11:12

spen123


2 Answers

There is no need to add data:image/png;base64, before, I tried using the code below, it works fine.

import base64
data = 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADBQTFRFA6b1q Ci5/f2lt/9yu3 Y8v2cMpb1/DSJbz5i9R2NLwfLrWbw m T8I8////////SvMAbAAAABB0Uk5T////////////////////AOAjXRkAAACYSURBVHjaLI8JDgMgCAQ5BVG3//9t0XYTE2Y5BPq0IGpwtxtTP4G5IFNMnmEKuCopPKUN8VTNpEylNgmCxjZa2c1kafpHSvMkX6sWe7PTkwRX1dY7gdyMRHZdZ98CF6NZT2ecMVaL9tmzTtMYcwbP y3XeTgZkF5s1OSHwRzo1fkILgWC5R0X4BHYu7t/136wO71DbvwVYADUkQegpokSjwAAAABJRU5ErkJggg=='.replace(' ', '+')
imgdata = base64.b64decode(data)
filename = 'some_image.jpg'  # I assume you have a way of picking unique filenames
with open(filename, 'wb') as f:
        f.write(imgdata)
like image 64
meelo Avatar answered Sep 17 '22 01:09

meelo


If you append data:image/png;base64, to data, then you get error. If You have this, you must replace it.

new_data = initial_data.replace('data:image/png;base64,', '')
like image 40
Felipe Cardona Avatar answered Sep 20 '22 01:09

Felipe Cardona