This repository has been archived on 2023-09-24. You can view files and clone it, but cannot push or open issues or pull requests.
takahe/core/files.py

25 lines
625 B
Python
Raw Normal View History

2022-12-02 02:46:49 +01:00
import io
import blurhash
from django.core.files import File
from PIL import Image, ImageOps
def resize_image(image: File, *, size: tuple[int, int]) -> File:
"""
Resizes an image to fit insize the given size (cropping one dimension
to fit if needed)
"""
with Image.open(image) as img:
resized_image = ImageOps.fit(img, size)
new_image_bytes = io.BytesIO()
resized_image.save(new_image_bytes, format=img.format)
return File(new_image_bytes)
def blurhash_image(image) -> str:
"""
Returns the blurhash for an image
"""
return blurhash.encode(image, 4, 4)