-
Notifications
You must be signed in to change notification settings - Fork 0
/
resize.py
executable file
·48 lines (40 loc) · 1.51 KB
/
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
"""resize.py: Resize a JPEG to to the lowest resolution suitable for 8"x10" printing, preserving all EXIF information."""
__author__ = 'Mike Maraya'
__copyright__ = 'Copyright (c) 2014, Mike Maraya'
__license__ = 'MIT'
__version__ = '1.0'
__email__ = '[email protected]'
__status__ = 'Development'
__maintainer__ = 'Mike Maraya'
import glob
import os
import sys
from PIL import Image
# maximum dimensions in pixels
hsize = 3008
vsize = 2000
for pattern in (sys.argv[1:]):
for filename in glob.glob(pattern):
try:
im = Image.open(filename)
except IOError:
print 'Could not read image properties for %s' % (filename)
# check for image orientation
if im.size[0] >= im.size[1]:
size = (hsize, vsize)
else:
size = (vsize, hsize)
# save information about the image before we change it
old_size = im.size
exif = im.info['exif']
# resize if either horizontal or vertical exceeds our maximum dimensions
if im.size[0] > size[0] or im.size[1] > size[1]:
try:
im.thumbnail(size, Image.ANTIALIAS)
im.save(filename, 'JPEG', exif=exif)
except IOError:
print 'Could not save resized image file %s' % (filename)
print '%s resized from %dx%d to %dx%d' % (filename, old_size[0], old_size[1], im.size[0], im.size[1])
else:
print '%s is %dx%d not resized' % (filename, im.size[0], im.size[1])