Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The method to obtain the width and height of an image tensor produced by tf.image.decode_jpeg is as follows:

  1. Decode the JPEG image using tf.image.decode_jpeg function. This function returns a tensor of shape [height, width, channels].

  2. 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.

  3. 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.

  4. Convert the height and width tensors to integers using tf.cast function.

  5. 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())