Mercurial > hg > cropresize
comparison cropresize/__init__.py @ 4:6ec33e2ce60f
more documentation
author | k0s <k0scist@gmail.com> |
---|---|
date | Sat, 06 Mar 2010 13:22:31 -0500 |
parents | 0a54e5bd2875 |
children | c89738f3f417 |
comparison
equal
deleted
inserted
replaced
3:2be0070c6f95 | 4:6ec33e2ce60f |
---|---|
4 from PIL import Image | 4 from PIL import Image |
5 | 5 |
6 def crop_resize(image, size, exact_size=False): | 6 def crop_resize(image, size, exact_size=False): |
7 """ | 7 """ |
8 Crop out the proportional middle of the image and set to the desired size. | 8 Crop out the proportional middle of the image and set to the desired size. |
9 * image: a PIL image object | |
10 * size: a 2-tuple of (width,height); at least one must be specified | |
11 * exact_size: whether to scale up for smaller images | |
9 If the image is bigger than the sizes passed, this works as expected. | 12 If the image is bigger than the sizes passed, this works as expected. |
10 If the image is smaller than the sizes passed, then behavior is dictated | 13 If the image is smaller than the sizes passed, then behavior is dictated |
11 by the `exact_size` flag. If the | 14 by the ``exact_size`` flag. If the ``exact_size`` flag is false, |
12 | 15 the image will be returned unmodified. If the ``exact_size`` flag is true, |
16 the image will be scaled up to the required size. | |
13 """ | 17 """ |
14 assert size[0] or size[1] | 18 assert size[0] or size[1] |
15 | 19 |
16 size = list(size) | 20 size = list(size) |
17 | 21 |
45 | 49 |
46 return image.resize(size, Image.ANTIALIAS) | 50 return image.resize(size, Image.ANTIALIAS) |
47 | 51 |
48 def main(): | 52 def main(): |
49 from optparse import OptionParser | 53 from optparse import OptionParser |
50 parser = OptionParser() | 54 parser = OptionParser('%prog [options] image1.png [image2.jpg] [...]') |
51 parser.add_option('-W', '--width') | 55 parser.add_option('-W', '--width') |
52 parser.add_option('-H', '--height') | 56 parser.add_option('-H', '--height') |
53 parser.add_option('-e', '--exact-size', dest='exact', action='store_true', default=False) | 57 parser.add_option('-e', '--exact-size', dest='exact', action='store_true', default=False) |
54 (options, args) = parser.parse_args() | 58 (options, args) = parser.parse_args() |
55 | 59 |