annotate cropresize/__init__.py @ 5:c89738f3f417

* improve command line interface * improve README * bump version
author k0s <k0scist@gmail.com>
date Sat, 06 Mar 2010 14:55:04 -0500
parents 6ec33e2ce60f
children 0cd9a1362310
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
2
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
3 import sys
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
4 from PIL import Image
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
5
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
6 def crop_resize(image, size, exact_size=False):
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
7 """
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
8 Crop out the proportional middle of the image and set to the desired size.
4
6ec33e2ce60f more documentation
k0s <k0scist@gmail.com>
parents: 0
diff changeset
9 * image: a PIL image object
6ec33e2ce60f more documentation
k0s <k0scist@gmail.com>
parents: 0
diff changeset
10 * size: a 2-tuple of (width,height); at least one must be specified
6ec33e2ce60f more documentation
k0s <k0scist@gmail.com>
parents: 0
diff changeset
11 * exact_size: whether to scale up for smaller images
0
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
12 If the image is bigger than the sizes passed, this works as expected.
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
13 If the image is smaller than the sizes passed, then behavior is dictated
4
6ec33e2ce60f more documentation
k0s <k0scist@gmail.com>
parents: 0
diff changeset
14 by the ``exact_size`` flag. If the ``exact_size`` flag is false,
6ec33e2ce60f more documentation
k0s <k0scist@gmail.com>
parents: 0
diff changeset
15 the image will be returned unmodified. If the ``exact_size`` flag is true,
6ec33e2ce60f more documentation
k0s <k0scist@gmail.com>
parents: 0
diff changeset
16 the image will be scaled up to the required size.
0
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
17 """
5
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
18 assert size[0] or size[1], "Must provide a width or a height"
0
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
19
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
20 size = list(size)
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
21
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
22 image_ar = image.size[0]/float(image.size[1])
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
23 crop = size[0] and size[1]
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
24 if not size[1]:
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
25 size[1] = int(image.size[1]*size[0]/float(image.size[0]) )
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
26 if not size[0]:
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
27 size[0] = int(image.size[0]*size[1]/float(image.size[1]) )
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
28 size_ar = size[0]/float(size[1])
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
29
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
30 if size[0] > image.size[0]:
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
31 if size[1] > image.size[1]:
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
32 if not exact_size:
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
33 return image
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
34 else:
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
35 pass
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
36 # raise NotImplementedError
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
37 elif size[1] > image.size[1]:
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
38 pass
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
39
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
40 if crop:
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
41 if image_ar > size_ar:
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
42 # trim the width
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
43 xoffset = int(0.5*(image.size[0] - size_ar*image.size[1]))
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
44 image = image.crop((xoffset, 0, image.size[0]-xoffset, image.size[1]))
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
45 elif image_ar < size_ar:
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
46 # trim the height
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
47 yoffset = int(0.5*(image.size[1] - image.size[0]/size_ar))
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
48 image = image.crop((0, yoffset, image.size[0], image.size[1] - yoffset))
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
49
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
50 return image.resize(size, Image.ANTIALIAS)
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
51
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
52 def main():
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
53 from optparse import OptionParser
4
6ec33e2ce60f more documentation
k0s <k0scist@gmail.com>
parents: 0
diff changeset
54 parser = OptionParser('%prog [options] image1.png [image2.jpg] [...]')
5
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
55 parser.add_option('-W', '--width',
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
56 help="desired width of image in pixels")
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
57 parser.add_option('-H', '--height',
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
58 help="desired height of image in pixels")
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
59 parser.add_option('-e', '--exact-size', dest='exact',
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
60 action='store_true', default=False,
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
61 help="scale up images smaller than specified")
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
62 parser.add_option('-d', '--display', dest='display',
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
63 action='store_true', default=False,
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
64 help="display the resized images (don't write to file)")
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
65 parser.add_option('-O', '--file', dest='output',
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
66 help="output to a file, stdout otherwise [1 image only]")
0
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
67 (options, args) = parser.parse_args()
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
68
5
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
69 # print arguments if files not given
0
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
70 if not args:
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
71 parser.print_help()
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
72 sys.exit()
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
73
5
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
74 # get the desired size
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
75 try:
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
76 width = int(options.width)
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
77 except TypeError:
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
78 width = None
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
79 try:
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
80 height = int(options.height)
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
81 except TypeError:
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
82 height = None
0
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
83
5
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
84 # asser that we have something to do with the image
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
85 if not options.display:
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
86 if len(args) > 1:
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
87 raise NotImplementedError # XXX
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
88
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
89 # resize the images
0
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
90 for arg in args:
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
91 image = Image.open(arg)
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
92 new_image = crop_resize(image, (width, height), options.exact)
5
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
93 if options.display:
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
94 new_image.show()
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
95 else:
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
96 if len(args) == 1:
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
97 # output
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
98 if options.output:
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
99 new_image.save(options.output)
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
100 else:
c89738f3f417 * improve command line interface
k0s <k0scist@gmail.com>
parents: 4
diff changeset
101 sys.stdout.write(new_image.tostring(image.format))
0
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
102
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
103 if __name__ == '__main__':
0a54e5bd2875 initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff changeset
104 main()