def letterbox_image(image, size,fillValue=[128,128,128]):
    '''
    resize image with unchanged aspect ratio using padding
    '''
    image_h, image_w = image.shape[:2]
    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 = cv2.resize(image,(new_w,new_h))
    if fillValue is None:
       fillValue = [int(x.mean()) for x in cv2.split(np.array(image))]
    boxed_image = np.zeros((size[1],size[0],3),dtype=np.uint8)
    boxed_image[:] = fillValue
    boxed_image[:new_h,:new_w,:] =resized_image 

    return boxed_image,new_w/image_w

    resize image with unchanged aspect ratio using padding
    
apphelper/image.py