-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new field to convert images to webp
- Loading branch information
Showing
7 changed files
with
50 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import os | ||
from io import BytesIO | ||
|
||
from django.core.exceptions import ValidationError | ||
from django.db import models | ||
from PIL import Image | ||
|
||
class ImageField(models.ImageField): | ||
""" | ||
A subclass of Django's ImageField that convert the image to a webp format | ||
""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
self.convert_to_webp = kwargs.pop("convert_to_webp", True) | ||
super().__init__(*args, **kwargs) | ||
|
||
def pre_save(self, model_instance, add): | ||
file = super().pre_save(model_instance, add) | ||
if self.convert_to_webp: | ||
try: | ||
img = Image.open(file) | ||
if img.format != "WEBP": | ||
buffer = BytesIO() | ||
img.save(buffer, format="WEBP") | ||
buffer.seek(0) | ||
file.save( | ||
os.path.basename(os.path.splitext(file.name)[0]) + ".webp", | ||
buffer, | ||
save=False, | ||
) | ||
except Exception as e: | ||
raise ValidationError(f"Invalid image file: {e}") from e | ||
return file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters