summaryrefslogtreecommitdiff
path: root/app/media/utils.py
diff options
context:
space:
mode:
authorluxagraf <sng@luxagraf.net>2020-12-02 12:07:25 -0500
committerluxagraf <sng@luxagraf.net>2020-12-02 12:07:25 -0500
commit550127b86684e65277aa27cdb068308c820d5abb (patch)
treee907f9e5e96fbd2d0746046cf5f7dd1ff9535719 /app/media/utils.py
parent50a852c09f35fa3ce655b7979c179617a323eb63 (diff)
added media app and migrations to move photo data
Diffstat (limited to 'app/media/utils.py')
-rw-r--r--app/media/utils.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/app/media/utils.py b/app/media/utils.py
new file mode 100644
index 0000000..893663c
--- /dev/null
+++ b/app/media/utils.py
@@ -0,0 +1,26 @@
+import os
+import subprocess
+
+from PIL import ImageFile
+# pip install python-resize-image
+from resizeimage import resizeimage
+
+
+def resize_image(img, width=None, height=None, quality=72, filepath=""):
+ """
+ given an image object, size, and filepath
+ resize the image, then save it , size, and filepath
+ resize the image, then save it at the filepath
+ """
+ base_path = os.path.dirname(filepath)
+ if not os.path.isdir(base_path):
+ os.makedirs(base_path)
+ if width and height:
+ newimg = resizeimage.resize_cover(img, [width, height])
+ if width and not height:
+ newimg = resizeimage.resize_width(img, width)
+ if height and not width:
+ newimg = resizeimage.resize_height(img, height)
+ ImageFile.MAXBLOCK = img.size[0] * img.size[1] * 4
+ newimg.save(filepath, newimg.format, quality=quality)
+ subprocess.call(["jpegoptim", "%s" % filepath])