annotate bin/recompress @ 92:03b02463cc16

omg python, recompresses image files. Because mplayer's png writer doesn't write compressed png.
author Edho Prima Arief <edho@myconan.net>
date Thu, 14 Jul 2011 00:34:46 +0700
parents
children 4f954c1e2fdc
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
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 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
15 im = Image.open(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
16 if im.format == "PNG":
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 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
18 im.save(filename, optimize=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 except:
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 retcode = 3
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 else:
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 retcode = 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
23 except:
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 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
25 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
26
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 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
28 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
29 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
30 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
31 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
32 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
33 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
34 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
35
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 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
37 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
38 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
39 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
40 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
41 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
42 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
43 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
44 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
45 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
46 elif 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
47 print "Not an image or corrupt 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
48 elif re == 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
49 print "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
50 elif re == 3:
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
51 print "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
52 except:
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
53 print "(%s: %s)" % (sys.exc_type, sys.exc_value)
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
54 print "Not a file or no read 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
55 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
56 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
57