file-upload - 值错误 : Fileobj must implement read

标签 file-upload amazon-s3 iplimage

我正在使用 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/

相关文章:

opencv - 在OpenCV中使用libjpeg将IplImage压缩为JPEG

c++ - IplImage 与 CvMat

php - Codeigniter 上传文件不起作用

file-upload - 3.5 Wordpress media uploader 手动实现

amazon-s3 - 对象存储有哪些用例?

amazon-s3 - 将s3数据迁移到谷歌云存储

c++ - Opencv cvConvertScale 面临的问题

java - RichFaces fileUpload 无法调用监听器

servlets - 使用 java servlet 上传到临时文件时出现 java.io.FileNotFoundException

amazon-web-services - 如何使用 pyspark 从 s3 存储桶中读取 csv 文件