def _resize_image self filename size Resizes the image to specified wi

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def _resize_image(self, filename, size):
'''
Resizes the image to specified width, height and force option
- filename: full path of image to resize
- size: dictionary containing:
- width: new width
- height: new height
- force: if True, image will be cropped to fit the exact size,
if False, it will have the bigger size that fits the specified
size, but without cropping, so it could be smaller on width or height
'''
WIDTH, HEIGHT = 0, 1
from PIL import Image, ImageOps
img = Image.open(filename)
if img.size[WIDTH] > size[0] or img.size[HEIGHT] > size[1]:
if size[2]:
img = ImageOps.fit(img, (size[0], size[1]), Image.ANTIALIAS)
else:
img.thumbnail((size[0], size[1]), Image.ANTIALIAS)
try:
img.save(filename, optimize=1)
except IOError:
img.save(filename)