I have this site I am making for my school and I need to optimize it, like a lot.
So I decided to serve all my images compressed and in next-gen formats like jpeg-2000 and webp. Using Pillow, this is what I have so far:
class Bulletin(models.Model):
banner = models.ImageField(upload_to='banner/', blank=True)
def save(self, *args, **kwargs):
super().save()
if self.banner:
thumbnail = Image.open(self.banner.path)
resized = thumbnail.resize((1280, 620))
resized.save(self.banner.path, quality=60)
So I think that this compresses the image (plz tell me if I made mistake with code above).
So now I want to be able to save this image in multiple formats I want the one uploaded image to be saved in these formats:
- webp
- jpeg2000
- jpeg
I am thinking of creating more fields on my model banner, like the field banner_webp
, and then I would just convert it to webp and during save I would save the converted image to that field.
The problem is that I don’t know how to convert image using Pillow, or how to do what I am asking. Thanks for help.