# HG changeset patch # User Edho Prima Arief # Date 1310579504 -25200 # Node ID 65ce115fd21d15c80d7f29cf8beff44cbff4383e # Parent a5324d2cc0eba0d7a26e25bb976976139bb6fa41# Parent 26c9dc4820519a6b10e2eb91e21100cee6b2414c Merge. diff -r 26c9dc482051 -r 65ce115fd21d bin/recompress --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bin/recompress Thu Jul 14 00:51:44 2011 +0700 @@ -0,0 +1,40 @@ +#!/usr/bin/env python + +import sys, os +from PIL import Image + + +# Raises a generic exception (exception.Exception) if input is not a PNG file. +def repng(filename): + im = Image.open(filename) + if im.format == "PNG": + im.save(filename, "PNG", optimize=1) + else: + raise Exception("Not a PNG image file") + +def hbytes(inbyte): + units = ["B", "kB", "MB", "GB", "TB", "PB"] + outbyte = float(inbyte) + current_unit = 0 + while outbyte > 1000: + outbyte /= 1000 + current_unit += 1 + return "%.2f %s" % (outbyte, units[current_unit]) + +if __name__ == "__main__": + for filename in sys.argv[1:]: + print "Recompressing %s:" % filename, + sys.stdout.flush() + try: + byte_orig = os.path.getsize(filename) + repng(filename) + byte_new = os.path.getsize(filename) + print "OK (%s => %s, %.2f%% saving)" % (hbytes(byte_orig), hbytes(byte_new), (byte_orig - byte_new)*100/float(byte_orig)), + re = 0 + except: + print "Failed", + print "(%s: %s)" % (sys.exc_type, sys.exc_value), + re = 1 + finally: + print "[%s]" % re +