我正在使用PIL库将gif图像转换为jpg,并将其上传到s3。我收到此错误:

ValueError:Fileobj必须实现读取

这是读取图像并转换为jpg的代码的一部分:

im = Image.open(url)
i = 0
mypalette = im.getpalette()
try:
    while 1:
            im.putpalette(mypalette)
            new_im = Image.new("RGBA", im.size)
            new_im.paste(im)
            key = 'img'+str(i)+'.jpg'
            in_mem_file = io.BytesIO()
            new_im.save(in_mem_file, "JPEG")
            s3.upload_fileobj(in_mem_file.getvalue(), bucket, key)
            i += 1
            im.seek(im.tell() + 1)
except EOFError:
            pass


我使用io.BytesIO读取字节,但是仍然收到相同的错误。如果我从in_mem_file中删除getvalue,则空图像将保存在存储桶中。

最佳答案

将图像重新保存到BytesIO缓冲区

with BytesIO() as in_mem_file:
    image.save(in_mem_file, format=image.format)
    in_mem_file.seek(0)
    s3client.upload_fileobj(in_mem_file, BUCKET, path)

08-07 22:39
查看更多