tf.image.resize_with_crop_or_pad

Crops and/or pads an image to a target width and height.

Used in the notebooks

Used in the tutorials

Resizes an image to a target width and height by either centrally cropping the image or padding it evenly with zeros.

If width or height is greater than the specified target_width or target_height respectively, this op centrally crops along that dimension.

For example:

image = np.arange(75).reshape(5, 5, 3)  # create 3-D image input
image[:,:,0]  # print first channel just for demo purposes
array([[ 0,  3,  6,  9, 12],
       [15, 18, 21, 24, 27],
       [30, 33, 36, 39, 42],
       [45, 48, 51, 54, 57],
       [60, 63, 66, 69, 72]])
image = tf.image.resize_with_crop_or_pad(image, 3, 3)  # crop
# print first channel for demo purposes; centrally cropped output
image[:,:,0]
<tf.Tensor: shape=(3, 3), dtype=int64, numpy=
array([[18, 21, 24],
       [33, 36, 39],
       [48, 51, 54]])>

If width or height is smaller than the specified target_width or target_height respectively, this op centrally pads with 0 along that dimension.