Mercurial > hg > cropresize
annotate cropresize/__init__.py @ 8:eb0f4870a019 default tip
depend on pillow which exists
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Tue, 03 Nov 2020 08:04:33 -0800 |
parents | 0cd9a1362310 |
children |
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 |
6
0cd9a1362310
fix bug where PIL lives in different places
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
4 try: |
0cd9a1362310
fix bug where PIL lives in different places
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
5 import Image |
0cd9a1362310
fix bug where PIL lives in different places
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
6 except ImportError: |
0cd9a1362310
fix bug where PIL lives in different places
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
7 from PIL import Image |
0
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
8 |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
9 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
|
10 """ |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
11 Crop out the proportional middle of the image and set to the desired size. |
4 | 12 * image: a PIL image object |
13 * size: a 2-tuple of (width,height); at least one must be specified | |
14 * 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
|
15 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
|
16 If the image is smaller than the sizes passed, then behavior is dictated |
4 | 17 by the ``exact_size`` flag. If the ``exact_size`` flag is false, |
18 the image will be returned unmodified. If the ``exact_size`` flag is true, | |
19 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
|
20 """ |
5 | 21 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
|
22 |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
23 size = list(size) |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
24 |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
25 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
|
26 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
|
27 if not size[1]: |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
28 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
|
29 if not size[0]: |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
30 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
|
31 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
|
32 |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
33 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
|
34 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
|
35 if not exact_size: |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
36 return image |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
37 else: |
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 # raise NotImplementedError |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
40 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
|
41 pass |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
42 |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
43 if crop: |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
44 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
|
45 # trim the width |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
46 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
|
47 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
|
48 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
|
49 # trim the height |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
50 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
|
51 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
|
52 |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
53 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
|
54 |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
55 def main(): |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
56 from optparse import OptionParser |
4 | 57 parser = OptionParser('%prog [options] image1.png [image2.jpg] [...]') |
5 | 58 parser.add_option('-W', '--width', |
59 help="desired width of image in pixels") | |
60 parser.add_option('-H', '--height', | |
61 help="desired height of image in pixels") | |
62 parser.add_option('-e', '--exact-size', dest='exact', | |
63 action='store_true', default=False, | |
64 help="scale up images smaller than specified") | |
65 parser.add_option('-d', '--display', dest='display', | |
66 action='store_true', default=False, | |
67 help="display the resized images (don't write to file)") | |
68 parser.add_option('-O', '--file', dest='output', | |
69 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
|
70 (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
|
71 |
5 | 72 # 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
|
73 if not args: |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
74 parser.print_help() |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
75 sys.exit() |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
76 |
5 | 77 # get the desired size |
78 try: | |
79 width = int(options.width) | |
80 except TypeError: | |
81 width = None | |
82 try: | |
83 height = int(options.height) | |
84 except TypeError: | |
85 height = None | |
0
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
86 |
5 | 87 # asser that we have something to do with the image |
88 if not options.display: | |
89 if len(args) > 1: | |
90 raise NotImplementedError # XXX | |
91 | |
92 # resize the images | |
0
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
93 for arg in args: |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
94 image = Image.open(arg) |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
95 new_image = crop_resize(image, (width, height), options.exact) |
5 | 96 if options.display: |
97 new_image.show() | |
98 else: | |
99 if len(args) == 1: | |
100 # output | |
101 if options.output: | |
102 new_image.save(options.output) | |
103 else: | |
104 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
|
105 |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
106 if __name__ == '__main__': |
0a54e5bd2875
initial import of cropresive from https://svn.openplans.org/svn/standalone/cropresize
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
107 main() |