def letterbox(image, shape):
 assert (len(image.shape) == 3)
 assert (len(shape) == 2)
 assert (image.shape[0] <= shape[0])
 assert (image.shape[1] <= shape[1])
 if ((image.shape[0] == shape[0]) and (image.shape[1] == shape[1])):
  return image.copy()
 rval = np.zeros((shape[0], shape[1], image.shape[2]), dtype=image.dtype)
 rstart = ((shape[0] - image.shape[0]) / 2)
 cstart = ((shape[1] - image.shape[1]) / 2)
 rend = (rstart + image.shape[0])
 cend = (cstart + image.shape[1])
 rval[rstart:rend, cstart:cend] = image
 return rval

def letterbox ( image , shape ) : assert ( len ( image . shape ) == 3 ) assert ( len ( shape ) == 2 ) assert ( image . shape [ 0 ] <= shape [ 0 ] ) assert ( image . shape [ 1 ] <= shape [ 1 ] ) if ( ( image . shape [ 0 ] == shape [ 0 ] ) and ( image . shape [ 1 ] == shape [ 1 ] ) ) : return image . copy ( ) rval = np . zeros ( ( shape [ 0 ] , shape [ 1 ] , image . shape [ 2 ] ) , dtype = image . dtype ) rstart = ( ( shape [ 0 ] - image . shape [ 0 ] ) / 2 ) cstart = ( ( shape [ 1 ] - image . shape [ 1 ] ) / 2 ) rend = ( rstart + image . shape [ 0 ] ) cend = ( cstart + image . shape [ 1 ] ) rval [ rstart : rend , cstart : cend ] = image return rval
pads image with black letterboxing to bring image .