Compress images with Pillow in Django
You can override save
method of the model:
from PIL import Image
class Photo(models.Model):
name = models.CharField(max_length=100, null=True, blank=True, verbose_name=_("Name"))
album = models.ForeignKey(Album, on_delete=models.PROTECT, related_name='photos', verbose_name=_("Album"))
photo = models.ImageField(verbose_name=_("Photo"))
def save(self, *args, **kwargs):
instance = super(Photo, self).save(*args, **kwargs)
image = Image.open(instance.photo.path)
image.save(instance.photo.path,quality=20,optimize=True)
return instance