def fit_inside(image, shape):
 assert (len(image.shape) == 3)
 assert (len(shape) == 2)
 if ((image.shape[0] <= shape[0]) and (image.shape[1] <= shape[1])):
  return image.copy()
 row_ratio = (float(image.shape[0]) / float(shape[0]))
 col_ratio = (float(image.shape[1]) / float(shape[1]))
 if (row_ratio > col_ratio):
  target_shape = [shape[0], min((image.shape[1] / row_ratio), shape[1])]
 else:
  target_shape = [min((image.shape[0] / col_ratio), shape[0]), shape[1]]
 assert (target_shape[0] <= shape[0])
 assert (target_shape[1] <= shape[1])
 assert ((target_shape[0] == shape[0]) or (target_shape[1] == shape[1]))
 rval = rescale(image, target_shape)
 return rval

def fit_inside ( image , shape ) : assert ( len ( image . shape ) == 3 ) assert ( len ( shape ) == 2 ) if ( ( image . shape [ 0 ] <= shape [ 0 ] ) and ( image . shape [ 1 ] <= shape [ 1 ] ) ) : return image . copy ( ) row_ratio = ( float ( image . shape [ 0 ] ) / float ( shape [ 0 ] ) ) col_ratio = ( float ( image . shape [ 1 ] ) / float ( shape [ 1 ] ) ) if ( row_ratio > col_ratio ) : target_shape = [ shape [ 0 ] , min ( ( image . shape [ 1 ] / row_ratio ) , shape [ 1 ] ) ] else : target_shape = [ min ( ( image . shape [ 0 ] / col_ratio ) , shape [ 0 ] ) , shape [ 1 ] ] assert ( target_shape [ 0 ] <= shape [ 0 ] ) assert ( target_shape [ 1 ] <= shape [ 1 ] ) assert ( ( target_shape [ 0 ] == shape [ 0 ] ) or ( target_shape [ 1 ] == shape [ 1 ] ) ) rval = rescale ( image , target_shape ) return rval
scales image down to fit inside shape preserves proportions of image parameters image : writeme shape : writeme returns writeme .