Skip to main content
 首页 » 编程设计

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

2024年10月25日11lidabo

我正在编写一个脚本来调整图像大小,然后将其上传到 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

请您参考如下方法:

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

    'image': temp.read(), 

到

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