(image, size)
return new_image,nw,nh
resize image with unchanged aspect ratio using padding
resize image with unchanged aspect ratio using padding
resize image with unchanged aspect ratio using padding
def letterbox_image(image, size):
    '''resize image with unchanged aspect ratio using padding'''
    iw, ih = image.size
    w, h = size
    scale = min(w/iw, h/ih)
    nw = int(iw*scale)
    nh = int(ih*scale)

    image = image.resize((nw,nh), Image.BICUBIC)
    new_image = Image.new('RGB', size, (0,0,0))
    new_image.paste(image, ((w-nw)//2, (h-nh)//2))
    return new_image,nw,nh
def letterbox_image ( image , size ) : iw , ih = image . size w , h = size scale = min ( w / iw , h / ih ) nw = int ( iw * scale ) nh = int ( ih * scale ) image = image . resize ( ( nw , nh ) , Image . BICUBIC ) new_image = Image . new ( 'RGB' , size , ( 0 , 0 , 0 ) ) new_image . paste ( image , ( ( w - nw ) // 2 , ( h - nh ) // 2 ) ) return new_image , nw , nh