comparison bin/recompress @ 113:9b91e702c19c

Latestest merge.
author Edho Prima Arief <edho@myconan.net>
date Wed, 19 Oct 2011 06:51:43 +0000
parents 97e24e89388f
children
comparison
equal deleted inserted replaced
88:917b525eaee0 113:9b91e702c19c
1 #!/usr/bin/env python
2
3 import sys, os
4 from PIL import Image
5
6
7 # Raises a generic exception (exception.Exception) if input is not a PNG file.
8 def repng(filename):
9 im = Image.open(filename)
10 if im.format == "PNG":
11 im.save(filename, "PNG", optimize=1)
12 else:
13 raise Exception("Not a PNG image file")
14
15 def hbytes(inbyte):
16 units = ["B", "kB", "MB", "GB", "TB", "PB"]
17 outbyte = float(inbyte)
18 current_unit = 0
19 while outbyte > 1000:
20 outbyte /= 1000
21 current_unit += 1
22 return "%.2f %s" % (outbyte, units[current_unit])
23
24 def sane_fn(filename):
25 import re
26 return re.sub(r"[\x00-\x1F\x7F\n\r]", "?", filename)
27
28 def main(files):
29 for filename in files:
30 print "Recompressing %s:" % sane_fn(filename),
31 sys.stdout.flush()
32 try:
33 byte_orig = os.path.getsize(filename)
34 repng(filename)
35 byte_new = os.path.getsize(filename)
36 print "OK (%s => %s, %.2f%% saving)" % (hbytes(byte_orig), hbytes(byte_new), (byte_orig - byte_new)*100/float(byte_orig)),
37 re = 0
38 except:
39 print "Failed",
40 print "(%s: %s)" % (sys.exc_type, sys.exc_value),
41 re = 1
42 finally:
43 print "[%s]" % re
44
45 if __name__ == "__main__":
46 main(sys.argv[1:])