Fix from ejpi
[gonvert] / support / scale.py
1 #!/usr/bin/env python
2
3 from __future__ import with_statement
4 from __future__ import division
5
6 import os
7 import logging
8
9 from PIL import Image
10
11
12 _moduleLogger = logging.getLogger(__name__)
13
14
15 def main(args):
16         import optparse
17         parser = optparse.OptionParser()
18         parser.add_option(
19                 "--input", dest="input",
20                 help="Input image to scale", metavar="INPUT"
21         )
22         parser.add_option(
23                 "--output", dest="output",
24                 help="Scaled image", metavar="OUTPUT"
25         )
26         parser.add_option(
27                 "--size", dest="size",
28                 help="Icon size", metavar="SIZE"
29         )
30         options, positional  = parser.parse_args(args)
31         if positional:
32                 parser.error("No positional arguments supported")
33         if None in [options.input, options.output, options.size]:
34                 parser.error("Missing argument")
35         if options.size == "guess":
36                 parts = reversed(os.path.split(options.output))
37                 for part in parts:
38                         try:
39                                 options.size = int(part)
40                                 _moduleLogger.info("Assuming image size of %r" % options.size)
41                                 break
42                         except ValueError:
43                                 pass
44
45         icon = Image.open(options.input)
46         icon.thumbnail((int(options.size), int(options.size)), Image.ANTIALIAS)
47         icon.save(options.output)
48
49
50 if __name__ == "__main__":
51         import sys
52         retcode = main(sys.argv[1:])
53         sys.exit(retcode)