2024-03-25 14:11:21 +00:00
|
|
|
from io import BytesIO
|
|
|
|
|
|
|
|
|
|
from django.core.files import File
|
2024-03-25 16:43:41 +00:00
|
|
|
from PIL import Image, ImageOps
|
2024-03-25 14:11:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _convert_image_to_webp(image_file):
|
|
|
|
|
image = Image.open(image_file)
|
2024-03-25 16:43:41 +00:00
|
|
|
ImageOps.exif_transpose(image, in_place=True)
|
2024-03-25 14:11:21 +00:00
|
|
|
image.convert("RGB")
|
|
|
|
|
image.thumbnail((486, 486))
|
|
|
|
|
thumb_io = BytesIO()
|
|
|
|
|
image.save(thumb_io, "WEBP")
|
|
|
|
|
return File(thumb_io, name=image_file.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def process_avatar_image(image_file):
|
|
|
|
|
return _convert_image_to_webp(image_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def process_post_image(image_file):
|
|
|
|
|
return _convert_image_to_webp(image_file)
|