5

I am converting gif images into jpgs using PIL library and uploading them to s3. I get this error:

ValueError: Fileobj must implement read

here is part of code that reads the image and convert to jpgs:

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

I use io.BytesIO to get read the bytes however I still get the same error. if I remove getvalue from in_mem_file empty images are saved in the bucket.

Qazal
  • 51
  • 1
  • 2

1 Answers1

5

Resave the image to BytesIO buffer

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)
DannyMoshe
  • 6,023
  • 4
  • 31
  • 53
ALAN ALBY
  • 76
  • 1
  • 7