def ltbx(im, requested_size, opts): img_x, img_y = [float(v) for v in im.size] dest_x, dest_y = [float(v) for v in requested_size] if 'ltbx' in opts and im.size != requested_size: img_ratio = img_x/img_y dest_ratio = dest_x/dest_y if dest_ratio > img_ratio: im = im.resize(( int(dest_y*img_ratio), dest_y ) , resample=Image.ANTIALIAS) else: im = im.resize(( dest_x, int(dest_x/img_ratio) ) , resample=Image.ANTIALIAS) canvas = Image.new("RGB", requested_size, THUMBNAIL_PADDING_COLOR) left = floor((requested_size[0] - im.size[0]) / 2) top = floor((requested_size[1] - im.size[1]) / 2) canvas.paste(im, (left, top)) im = canvas return im ltbx.valid_options = ('ltbx', )