Mercurial > ec-dotfiles
annotate bin/recompress @ 94:9790bc126ea6
Epic simplified error handling in true try/except/finally spirit.
author | Edho Prima Arief <edho@myconan.net> |
---|---|
date | Thu, 14 Jul 2011 00:49:33 +0700 |
parents | 4f954c1e2fdc |
children | a5324d2cc0eb |
rev | line source |
---|---|
92
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
2 |
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
3 import sys, os |
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
4 from PIL import Image |
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
5 |
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
6 |
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
7 # return codes: |
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
8 # 0 = ok |
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
9 # 1 = error opening file (not an image, not a file, unreadable or whatever) |
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
10 # 2 = error saving file (no write permission) |
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
11 # 3 = not a png file |
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
12 def repng(filename): |
93
4f954c1e2fdc
Stop being pretentious and only return 0 or 1 in case of success or failure. Everything else is handled by exception handler.
Edho Prima Arief <edho@myconan.net>
parents:
92
diff
changeset
|
13 im = Image.open(filename) |
4f954c1e2fdc
Stop being pretentious and only return 0 or 1 in case of success or failure. Everything else is handled by exception handler.
Edho Prima Arief <edho@myconan.net>
parents:
92
diff
changeset
|
14 if im.format == "PNG": |
94
9790bc126ea6
Epic simplified error handling in true try/except/finally spirit.
Edho Prima Arief <edho@myconan.net>
parents:
93
diff
changeset
|
15 im.save(filename, "PNG", optimize=1) |
93
4f954c1e2fdc
Stop being pretentious and only return 0 or 1 in case of success or failure. Everything else is handled by exception handler.
Edho Prima Arief <edho@myconan.net>
parents:
92
diff
changeset
|
16 else: |
94
9790bc126ea6
Epic simplified error handling in true try/except/finally spirit.
Edho Prima Arief <edho@myconan.net>
parents:
93
diff
changeset
|
17 raise Exception("Not a PNG image file") |
92
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
18 |
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
19 def hbytes(inbyte): |
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
20 units = ["B", "kB", "MB", "GB", "TB", "PB"] |
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
21 outbyte = float(inbyte) |
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
22 current_unit = 0 |
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
23 while outbyte > 1000: |
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
24 outbyte /= 1000 |
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
25 current_unit += 1 |
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
26 return "%.2f %s" % (outbyte, units[current_unit]) |
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
27 |
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
28 if __name__ == "__main__": |
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
29 for filename in sys.argv[1:]: |
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
30 print "Recompressing %s:" % filename, |
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
31 sys.stdout.flush() |
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
32 try: |
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
33 byte_orig = os.path.getsize(filename) |
94
9790bc126ea6
Epic simplified error handling in true try/except/finally spirit.
Edho Prima Arief <edho@myconan.net>
parents:
93
diff
changeset
|
34 repng(filename) |
9790bc126ea6
Epic simplified error handling in true try/except/finally spirit.
Edho Prima Arief <edho@myconan.net>
parents:
93
diff
changeset
|
35 byte_new = os.path.getsize(filename) |
9790bc126ea6
Epic simplified error handling in true try/except/finally spirit.
Edho Prima Arief <edho@myconan.net>
parents:
93
diff
changeset
|
36 print "OK (%s => %s, %.2f%% saving)" % (hbytes(byte_orig), hbytes(byte_new), (byte_orig - byte_new)*100/float(byte_orig)), |
9790bc126ea6
Epic simplified error handling in true try/except/finally spirit.
Edho Prima Arief <edho@myconan.net>
parents:
93
diff
changeset
|
37 re = 0 |
92
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
38 except: |
93
4f954c1e2fdc
Stop being pretentious and only return 0 or 1 in case of success or failure. Everything else is handled by exception handler.
Edho Prima Arief <edho@myconan.net>
parents:
92
diff
changeset
|
39 print "Failed", |
4f954c1e2fdc
Stop being pretentious and only return 0 or 1 in case of success or failure. Everything else is handled by exception handler.
Edho Prima Arief <edho@myconan.net>
parents:
92
diff
changeset
|
40 print "(%s: %s)" % (sys.exc_type, sys.exc_value), |
92
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
41 re = 1 |
94
9790bc126ea6
Epic simplified error handling in true try/except/finally spirit.
Edho Prima Arief <edho@myconan.net>
parents:
93
diff
changeset
|
42 finally: |
9790bc126ea6
Epic simplified error handling in true try/except/finally spirit.
Edho Prima Arief <edho@myconan.net>
parents:
93
diff
changeset
|
43 print "[%s]" % re |
92
03b02463cc16
omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
Edho Prima Arief <edho@myconan.net>
parents:
diff
changeset
|
44 |