annotate bin/recompress @ 123:173833e4ba35

Merge.
author Edho Prima Arief <edho@myconan.net>
date Wed, 26 Oct 2011 15:15:39 +0700
parents 97e24e89388f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
a5324d2cc0eb Updated documentation.
Edho Prima Arief <edho@myconan.net>
parents: 94
diff changeset
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
98
9be628347dce Worst thing about unix: it allows control character and newline in its filename. Added handler to ensure that shit doesn't pass through. Makes things slow but it can't be helped (tm).
Edho Prima Arief <edho@myconan.net>
parents: 95
diff changeset
24 def sane_fn(filename):
9be628347dce Worst thing about unix: it allows control character and newline in its filename. Added handler to ensure that shit doesn't pass through. Makes things slow but it can't be helped (tm).
Edho Prima Arief <edho@myconan.net>
parents: 95
diff changeset
25 import re
9be628347dce Worst thing about unix: it allows control character and newline in its filename. Added handler to ensure that shit doesn't pass through. Makes things slow but it can't be helped (tm).
Edho Prima Arief <edho@myconan.net>
parents: 95
diff changeset
26 return re.sub(r"[\x00-\x1F\x7F\n\r]", "?", filename)
9be628347dce Worst thing about unix: it allows control character and newline in its filename. Added handler to ensure that shit doesn't pass through. Makes things slow but it can't be helped (tm).
Edho Prima Arief <edho@myconan.net>
parents: 95
diff changeset
27
99
97e24e89388f explicit main call.
Edho Prima Arief <edho@myconan.net>
parents: 98
diff changeset
28 def main(files):
97e24e89388f explicit main call.
Edho Prima Arief <edho@myconan.net>
parents: 98
diff changeset
29 for filename in files:
98
9be628347dce Worst thing about unix: it allows control character and newline in its filename. Added handler to ensure that shit doesn't pass through. Makes things slow but it can't be helped (tm).
Edho Prima Arief <edho@myconan.net>
parents: 95
diff changeset
30 print "Recompressing %s:" % sane_fn(filename),
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
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
99
97e24e89388f explicit main call.
Edho Prima Arief <edho@myconan.net>
parents: 98
diff changeset
45 if __name__ == "__main__":
97e24e89388f explicit main call.
Edho Prima Arief <edho@myconan.net>
parents: 98
diff changeset
46 main(sys.argv[1:])