我正在使用 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)
关于file-upload - 值错误 : Fileobj must implement read,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46839858/