ocarina: Clean up image resize function

I now wrap the entire resize operation in a try / except block.
Hopefully this will help to catch some errors.  I also cleaned up my
"return early" conditions.
This commit is contained in:
Bryan Schumaker 2011-05-08 10:24:10 -04:00
parent 22168d2998
commit 0862c57b6a
1 changed files with 15 additions and 13 deletions

View File

@ -7,29 +7,31 @@ from libsaria import sources
def find_new_width(buf, new_h):
w = buf.get_width()
h = buf.get_height()
if new_h == 0:
return
if h == new_h:
return
if h == 0:
h = 1
return (float(w) / float(h)) * new_h
new_w = (float(w) / float(h)) * new_h
return int(new_w)
def resize(img, new_h):
try:
buf = img.get_pixbuf()
except Exception, e:
return
def _resize(img, new_h):
buf = img.get_pixbuf()
if buf == None:
return
new_w = find_new_width(buf, new_h)
if new_w == None:
return
buf = buf.scale_simple(int(new_w), int(new_h), gtk.gdk.INTERP_HYPER)
buf = buf.scale_simple(new_w, new_h, gtk.gdk.INTERP_HYPER)
if buf == None:
return
img.set_from_pixbuf(buf)
def resize(img, new_h):
if new_h == 0:
return
new_h = int(new_h)
try:
_resize(img, new_h)
except Exception, e:
print e
class FileImage(gtk.Image):
def __init__(self):