summaryrefslogtreecommitdiff
path: root/app/photos/utils.py
blob: 28047d48e3215840ae5065953d360ebd277f03d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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, base_path="", filename=""):
    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])