forked from CodingEZ/Automated-Garden
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResize.py
28 lines (22 loc) · 784 Bytes
/
Resize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from PIL import Image
def get_extension_index(name):
index = -1
while name[index] != '.' and index > -len(name) + 1:
index -= 1
return index
def resize_image(name, windowSize, imgFormat='JPEG'):
"""Downsizes image to fit parameters. Unable to stretch small images."""
img = Image.open(name)
index = get_extension_index(name)
extension = name[index:]
if img.size[0] > windowSize[0]:
width = windowSize[0]
else:
width = img.size[0]
if img.size[1] > windowSize[1]:
height = windowSize[1]
else:
height = img.size[1]
img = img.resize((width, height), Image.ANTIALIAS) # image resize filter
img.save(name[:index] + 'COPY' + extension, imgFormat)
return name[:index] + 'COPY' + extension