comparison 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
comparison
equal deleted inserted replaced
92:03b02463cc16 93:4f954c1e2fdc
9 # 1 = error opening file (not an image, not a file, unreadable or whatever) 9 # 1 = error opening file (not an image, not a file, unreadable or whatever)
10 # 2 = error saving file (no write permission) 10 # 2 = error saving file (no write permission)
11 # 3 = not a png file 11 # 3 = not a png file
12 def repng(filename): 12 def repng(filename):
13 retcode = 0 13 retcode = 0
14 try: 14 im = Image.open(filename)
15 im = Image.open(filename) 15 if im.format == "PNG":
16 if im.format == "PNG": 16 im.save(filename, optimize=1)
17 try: 17 else:
18 im.save(filename, optimize=1)
19 except:
20 retcode = 3
21 else:
22 retcode = 2
23 except:
24 retcode = 1 18 retcode = 1
25 return retcode 19 return retcode
26 20
27 def hbytes(inbyte): 21 def hbytes(inbyte):
28 units = ["B", "kB", "MB", "GB", "TB", "PB"] 22 units = ["B", "kB", "MB", "GB", "TB", "PB"]
42 re = repng(filename) 36 re = repng(filename)
43 if re == 0: 37 if re == 0:
44 byte_new = os.path.getsize(filename) 38 byte_new = os.path.getsize(filename)
45 print "OK (%s => %s, %.2f%% saving)" % (hbytes(byte_orig), hbytes(byte_new), (byte_orig - byte_new)*100/float(byte_orig)), 39 print "OK (%s => %s, %.2f%% saving)" % (hbytes(byte_orig), hbytes(byte_new), (byte_orig - byte_new)*100/float(byte_orig)),
46 elif re == 1: 40 elif re == 1:
47 print "Not an image or corrupt file", 41 print "Not a PNG image file",
48 elif re == 2:
49 print "No write permission",
50 elif re == 3:
51 print "Not a PNG file",
52 except: 42 except:
53 print "(%s: %s)" % (sys.exc_type, sys.exc_value) 43 print "Failed",
54 print "Not a file or no read permission", 44 print "(%s: %s)" % (sys.exc_type, sys.exc_value),
55 re = 1 45 re = 1
56 print "[%s]" % re 46 print "[%s]" % re
57 47