Having a Django model for a thumbnail:
class Thumb(models.Model): thumb = models.ImageField(upload_to='uploads/thumb/', null=True, default=None)
A sketch with the pillow package is created in the view and you should save it in a Thumb instance using the following code:
image.thumbnail((50, 50)) inst.thumb.save('thumb.jpg', ???)
What is the correct way to make image data for inst.thumb.save in ??? ?
I managed to get the work below:
thumb_temp = NamedTemporaryFile() image.save(thumb_temp, 'JPEG', quality=80) thumb_temp.flush() inst.thumb.save('thumb.jpg', File(thumb_temp)) thumb_temp.close() # Probably required to ensure temp file delete at close
But it seems quite inconvenient to write a temporary file to transfer internal data to inst.thumb.save , so I wonder if there is a more elegant way to do this. Documentation for the Django NamedTemporaryFile .
source share