Mercurial > ec-dotfiles
annotate bin/recompress @ 97:65ce115fd21d
Merge.
author | Edho Prima Arief <edho@myconan.net> |
---|---|
date | Thu, 14 Jul 2011 00:51:44 +0700 |
parents | a5324d2cc0eb |
children | 9be628347dce |
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 |
95 | 7 # Raises a generic exception (exception.Exception) if input is not a PNG 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
|
8 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
|
9 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
|
10 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
|
11 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
|
12 else: |
94
9790bc126ea6
Epic simplified error handling in true try/except/finally spirit.
Edho Prima Arief <edho@myconan.net>
parents:
93
diff
changeset
|
13 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
|
14 |
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
|
15 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
|
16 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
|
17 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
|
18 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
|
19 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
|
20 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
|
21 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
|
22 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
|
23 |
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 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
|
25 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
|
26 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
|
27 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
|
28 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
|
29 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
|
30 repng(filename) |
9790bc126ea6
Epic simplified error handling in true try/except/finally spirit.
Edho Prima Arief <edho@myconan.net>
parents:
93
diff
changeset
|
31 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
|
32 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
|
33 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
|
34 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
|
35 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
|
36 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
|
37 re = 1 |
94
9790bc126ea6
Epic simplified error handling in true try/except/finally spirit.
Edho Prima Arief <edho@myconan.net>
parents:
93
diff
changeset
|
38 finally: |
9790bc126ea6
Epic simplified error handling in true try/except/finally spirit.
Edho Prima Arief <edho@myconan.net>
parents:
93
diff
changeset
|
39 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
|
40 |