-
Notifications
You must be signed in to change notification settings - Fork 0
/
photoresize.py
37 lines (24 loc) · 1.14 KB
/
photoresize.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
29
30
31
32
33
34
35
36
37
#
# This script will resize all images in a directory to the MAXSIZE setting.
# It will keep the image dimensions proportional to the original image.
# Tom Swanson Esri 4/2/2013
from PIL import Image #download from here - http://www.pythonware.com/products/pil/
import os
#Input Directory
indir = "C:/Temp/PhotoIn"
outdir = "C:/Temp/PhotoOut"
MAXSIZE = 600 # Maximum width or height for your photo
files_in_dir = os.listdir(indir)
for file_in_dir in files_in_dir:
WIDTHSIZE = MAXSIZE
HEIGHTSIZE = MAXSIZE
im = Image.open(indir+"\\"+file_in_dir) # open the input file
(width, height) = im.size # get the size of the input image
if width > MAXSIZE or height > MAXSIZE: #only resize if height or width is greater than maxsize
if width > height:
HEIGHTSIZE = (MAXSIZE*height)/width
elif height > width:
WIDTHSIZE = (MAXSIZE*width)/height
print file_in_dir +" - "+str(width)+" - "+str(height)+" - "+str(WIDTHSIZE)+" - "+str(HEIGHTSIZE)
im = im.resize((WIDTHSIZE, HEIGHTSIZE), Image.ANTIALIAS)
im.save(outdir+"\\"+file_in_dir, "jpeg", quality = 100)