from functools import reduce from PIL import Image def compose(*funcs): if funcs: return reduce(lambda f, g: lambda *a, **kw: g(f(*a, **kw)), funcs) else: raise ValueError('Composition of empty sequence not supported.') def letterbox_image(image, size): image_w, image_h = image.size w, h = size new_w = int(image_w * min(w*1.0/image_w, h*1.0/image_h)) new_h = int(image_h * min(w*1.0/image_w, h*1.0/image_h)) resized_image = image.resize((new_w,new_h), Image.BICUBIC) boxed_image = Image.new('RGB', size, (128,128,128)) boxed_image.paste(resized_image, ((w-new_w)//2,(h-new_h)//2)) return boxed_image