summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorAndrew Godwin2022-12-01 18:46:49 -0700
committerAndrew Godwin2022-12-01 18:47:04 -0700
commit6f2f28a3a752cc47d9dc96bda862ed67cd75c9af (patch)
tree1165843b5c207d9e50028bf48db399df20cf7552 /core
parenta826ae18ea41e44614ce20d5caad9425d76aa60d (diff)
downloadtakahe-6f2f28a3a752cc47d9dc96bda862ed67cd75c9af.tar.gz
takahe-6f2f28a3a752cc47d9dc96bda862ed67cd75c9af.tar.bz2
takahe-6f2f28a3a752cc47d9dc96bda862ed67cd75c9af.zip
Image attachment uploads
Diffstat (limited to 'core')
-rw-r--r--core/files.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/core/files.py b/core/files.py
new file mode 100644
index 0000000..c4f4355
--- /dev/null
+++ b/core/files.py
@@ -0,0 +1,24 @@
+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)