[Answered ]-In Memory Image to Zipfile

2👍

Well I was kind of dumb. objective.picture.read() returns a byte string (really long byte string…) so I shouldn’t have used Image but ImageFile.Parser() and feed that byte string to the parser so it can return an Image that I can work with. Here is the code :

from PIL import ImageFile
from io import BytesIO

p = ImageFile.Parser()
p.feed(objective.picture.read())
image = p.close()
image_file = BytesIO()
exifdata = image.info['exif']
image.save(image_file, 'JPEG', quality=50, exif=exifdata)
# Here zf is a zipfile writer
zf.writestr(zipped_filename, image_file.getvalue())

The close() actually returns the image parsed from the bytestring.
Here is the doc : The ImageFile Documentation

👤Depado

Leave a comment