Mercurial > hg > cropresize
annotate cropresize/__init__.py @ 0:0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
author | k0s <k0scist@gmail.com> |
---|---|
date | Thu, 29 Oct 2009 18:28:13 -0400 |
parents | |
children | 6ec33e2ce60f |
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. |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
9 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
|
10 If the image is smaller than the sizes passed, then behavior is dictated |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
11 by the `exact_size` flag. If the |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
12 |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
13 """ |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
14 assert size[0] or size[1] |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
15 |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
16 size = list(size) |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
17 |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
18 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
|
19 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
|
20 if not size[1]: |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
21 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
|
22 if not size[0]: |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
23 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
|
24 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
|
25 |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
26 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
|
27 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
|
28 if not exact_size: |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
29 return image |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
30 else: |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
31 pass |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
32 # raise NotImplementedError |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
33 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
|
34 pass |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
35 |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
36 if crop: |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
37 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
|
38 # trim the width |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
39 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
|
40 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
|
41 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
|
42 # trim the height |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
43 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
|
44 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
|
45 |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
46 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
|
47 |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
48 def main(): |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
49 from optparse import OptionParser |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
50 parser = OptionParser() |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
51 parser.add_option('-W', '--width') |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
52 parser.add_option('-H', '--height') |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
53 parser.add_option('-e', '--exact-size', dest='exact', action='store_true', default=False) |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
54 (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
|
55 |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
56 if not args: |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
57 parser.print_help() |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
58 sys.exit() |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
59 |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
60 width = int(options.width) |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
61 height = int(options.height) |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
62 |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
63 for arg in args: |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
64 image = Image.open(arg) |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
65 new_image = crop_resize(image, (width, height), options.exact) |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
66 new_image.show() |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
67 |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
68 if __name__ == '__main__': |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
69 main() |