The method to obtain the width and height of an image tensor produced by tf.image.decode_jpeg is as follows:
Decode the JPEG image using tf.image.decode_jpeg function. This function returns a tensor of shape [height, width, channels].
Obtain the tensor shape using tf.shape function. This function returns a tensor of shape [3] containing the height, width and channels of the decoded image tensor.
Extract the height and width dimensions from the tensor shape using tf.slice function. The height and width dimensions are located at indices 0 and 1 respectively.
Convert the height and width tensors to integers using tf.cast function.
Use the obtained height and width values for further processing.
The code snippet for obtaining the height and width dimensions of an image tensor produced by tf.image.decode_jpeg is as follows:
import tensorflow as tf
# Decode JPEG image
image_raw = tf.io.read_file('image.jpeg')
image = tf.image.decode_jpeg(image_raw)
# Obtain tensor shape
shape = tf.shape(image)
# Extract height and width dimensions
height = tf.cast(tf.slice(shape, [0], [1]), tf.int32)
width = tf.cast(tf.slice(shape, [1], [1]), tf.int32)
# Print height and width values
print('Height:', height.numpy())
print('Width:', width.numpy())
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-08-22 11:00:00 +0000
Seen: 13 times
Last updated: Dec 20 '22
What is the method for aligning an image at the center using NPOI in c#?
How can I use iText7 to create a PDF that includes a pie chart?
What is causing my Flutter Android apk's image to appear as a single 1x1 pixel?
When image delivery is slow, how does it affect the inference time with GPU?
What is meant by "Kubernetes error invalid capacity 0 on image filesystem"?
Why isn't the CSS background image being displayed?
What is the method for showing a spinner on an Android Studio background image?