tf.image.resize_with_crop_or_pad
Stay organized with collections
Save and categorize content based on your preferences.
Crops and/or pads an image to a target width and height.
tf.image.resize_with_crop_or_pad(
image, target_height, target_width
)
Used in the notebooks
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.
For example:
image = np.arange(1, 28).reshape(3, 3, 3) # create 3-D image input
image[:,:,0] # print first channel just for demo purposes
array([[ 1, 4, 7],
[10, 13, 16],
[19, 22, 25]])
image = tf.image.resize_with_crop_or_pad(image, 5, 5) # pad
# print first channel for demo purposes; we should see 0 paddings
image[:,:,0]
<tf.Tensor: shape=(5, 5), dtype=int64, numpy=
array([[ 0, 0, 0, 0, 0],
[ 0, 1, 4, 7, 0],
[ 0, 10, 13, 16, 0],
[ 0, 19, 22, 25, 0],
[ 0, 0, 0, 0, 0]])>
Args |
image
|
4-D Tensor of shape [batch, height, width, channels] or 3-D Tensor
of shape [height, width, channels] .
|
target_height
|
Target height.
|
target_width
|
Target width.
|
Raises |
ValueError
|
if target_height or target_width are zero or negative.
|
Returns |
Cropped and/or padded image.
If images was 4-D, a 4-D float Tensor of shape
[batch, new_height, new_width, channels] .
If images was 3-D, a 3-D float Tensor of shape
[new_height, new_width, channels] .
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.image.resize_with_crop_or_pad\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/image_ops_impl.py#L1276-L1430) |\n\nCrops and/or pads an image to a target width and height.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.image.resize_image_with_crop_or_pad`](https://www.tensorflow.org/api_docs/python/tf/image/resize_with_crop_or_pad), [`tf.compat.v1.image.resize_with_crop_or_pad`](https://www.tensorflow.org/api_docs/python/tf/image/resize_with_crop_or_pad)\n\n\u003cbr /\u003e\n\n tf.image.resize_with_crop_or_pad(\n image, target_height, target_width\n )\n\n### Used in the notebooks\n\n| Used in the tutorials |\n|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Data augmentation](https://www.tensorflow.org/tutorials/images/data_augmentation) - [Artistic Style Transfer with TensorFlow Lite](https://www.tensorflow.org/lite/examples/style_transfer/overview) |\n\nResizes an image to a target width and height by either centrally\ncropping the image or padding it evenly with zeros.\n\nIf `width` or `height` is greater than the specified `target_width` or\n`target_height` respectively, this op centrally crops along that dimension.\n\n#### For example:\n\n image = np.arange(75).reshape(5, 5, 3) # create 3-D image input\n image[:,:,0] # print first channel just for demo purposes\n array([[ 0, 3, 6, 9, 12],\n [15, 18, 21, 24, 27],\n [30, 33, 36, 39, 42],\n [45, 48, 51, 54, 57],\n [60, 63, 66, 69, 72]])\n image = tf.image.resize_with_crop_or_pad(image, 3, 3) # crop\n # print first channel for demo purposes; centrally cropped output\n image[:,:,0]\n \u003ctf.Tensor: shape=(3, 3), dtype=int64, numpy=\n array([[18, 21, 24],\n [33, 36, 39],\n [48, 51, 54]])\u003e\n\nIf `width` or `height` is smaller than the specified `target_width` or\n`target_height` respectively, this op centrally pads with 0 along that\ndimension.\n\n#### For example:\n\n image = np.arange(1, 28).reshape(3, 3, 3) # create 3-D image input\n image[:,:,0] # print first channel just for demo purposes\n array([[ 1, 4, 7],\n [10, 13, 16],\n [19, 22, 25]])\n image = tf.image.resize_with_crop_or_pad(image, 5, 5) # pad\n # print first channel for demo purposes; we should see 0 paddings\n image[:,:,0]\n \u003ctf.Tensor: shape=(5, 5), dtype=int64, numpy=\n array([[ 0, 0, 0, 0, 0],\n [ 0, 1, 4, 7, 0],\n [ 0, 10, 13, 16, 0],\n [ 0, 19, 22, 25, 0],\n [ 0, 0, 0, 0, 0]])\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-----------------|------------------------------------------------------------------------------------------------------------|\n| `image` | 4-D Tensor of shape `[batch, height, width, channels]` or 3-D Tensor of shape `[height, width, channels]`. |\n| `target_height` | Target height. |\n| `target_width` | Target width. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|------------------------------------------------------------|\n| `ValueError` | if `target_height` or `target_width` are zero or negative. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| Cropped and/or padded image. If `images` was 4-D, a 4-D float Tensor of shape `[batch, new_height, new_width, channels]`. If `images` was 3-D, a 3-D float Tensor of shape `[new_height, new_width, channels]`. ||\n\n\u003cbr /\u003e"]]