generate image thumbnail as a file after uploading

+1 vote
asked Apr 4, 2017 by anonymous

what is the code to spawn/create an image thumbnail and save as a copy afterwards an image got uploaded using django form.

I am already using thumbor django package for this job but i want to save the thumbnail image as a file, so it can later be used directly without taking tension of cache or runtime generation of thumbnail.

I am already doing image.resize(size, ...) to create thumb but what is the best way, so it can also work on admin interface.

1 Answer

+1 vote
answered Apr 4, 2017 by anonymous

This is the fasted solution i come up with. you need PIL library and here u go

from PIL import Image
 class Model(models.Model):
        image = models.ImageField(upload_to='images')
        thumbnail = models.ImageField(upload_to='thumbs', editable=False)
        def save(self, *args, **kwargs):
            if self.image:
                self.thumbnail ="thumbnail_%s_%s" % (image, "_".join(size)))
            super(Model, self).save(*args, **kwargs)