Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: Fileobj must implement read

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.

like image 272
Qazal Avatar asked Oct 30 '22 00:10

Qazal


1 Answers

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)
like image 65
ALAN ALBY Avatar answered Dec 17 '22 06:12

ALAN ALBY