annotate bin/recompress @ 93:4f954c1e2fdc

Stop being pretentious and only return 0 or 1 in case of success or failure. Everything else is handled by exception handler.
author Edho Prima Arief <edho@myconan.net>
date Thu, 14 Jul 2011 00:39:09 +0700
parents 03b02463cc16
children 9790bc126ea6
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
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):
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
13 retcode = 0
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
14 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
15 if im.format == "PNG":
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 im.save(filename, optimize=1)
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
17 else:
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 retcode = 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
19 return retcode
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
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 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
22 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
23 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
24 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
25 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
26 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
27 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
28 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
29
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 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
31 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
32 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
33 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
34 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
35 byte_orig = os.path.getsize(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
36 re = repng(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
37 if re == 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
38 byte_new = os.path.getsize(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
39 print "OK (%s => %s, %.2f%% saving)" % (hbytes(byte_orig), hbytes(byte_new), (byte_orig - byte_new)*100/float(byte_orig)),
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 elif re == 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
41 print "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
42 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
43 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
44 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
45 re = 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
46 print "[%s]" % re
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
47