diff options
Diffstat (limited to 'app/media/utils.py')
-rw-r--r-- | app/media/utils.py | 24 |
1 files changed, 11 insertions, 13 deletions
diff --git a/app/media/utils.py b/app/media/utils.py index 84e72f5..893663c 100644 --- a/app/media/utils.py +++ b/app/media/utils.py @@ -1,28 +1,26 @@ import os -import re import subprocess -from django.apps import apps -from django.conf import settings - from PIL import ImageFile -from bs4 import BeautifulSoup # pip install python-resize-image from resizeimage import resizeimage -def resize_image(img, width=None, height=None, quality=72, base_path="", filename=""): +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) - if not os.path.isdir(base_path): - os.makedirs(base_path) - path = "%s%s" % (base_path, filename) ImageFile.MAXBLOCK = img.size[0] * img.size[1] * 4 - newimg.save(path, newimg.format, quality=quality) - subprocess.call(["jpegoptim", "%s" % path]) - - + newimg.save(filepath, newimg.format, quality=quality) + subprocess.call(["jpegoptim", "%s" % filepath]) |