将调整大小的图像上传到Imgur而不保存在磁盘上

时间:2014-10-14 15:43:35

标签: python python-imaging-library imgur

我正在编写一个脚本,它会调整图像大小,然后将其上传到Imgur - 但我不想将图像保存在磁盘上!

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'
    }
)

运行此脚本后,出现错误Image format not supported, or image is corrupt

1 个答案:

答案 0 :(得分:2)

您的图片似乎不是base64编码,请尝试更改:

    'image': temp.read(),

    'image': temp.read().encode("base64"),
相关问题