Search code examples
pythonpython-imaging-libraryimgur

Upload Resized Image To Imgur Without Saving On Disk


I am writing a script that will resize an image and then upload it to Imgur - but I don't want to save the image on disk!

fp = requests.get(link_of_image)
img = StringIO.StringIO(fp.content)
image = Image.open(img)
im2 = image.resize((50, 50), Image.ANTIALIAS)
temp = StringIO.StringIO()
im2.save(temp, 'png')
temp.seek(0)

j1 = requests.post(
    url,
    headers = headers,
    data = {
        'key': api_key,
        'image': temp.read(),
        'type': 'base64',
        'name': '1.png',
        'title': 'Picture no. 1'
    }
)

After running this script, I get an error Image format not supported, or image is corrupt


Solution

  • seems your image is not base64 encoded, try changing:

        'image': temp.read(),
    

    to

        'image': temp.read().encode("base64"),