Ask Your Question
2

What is the method to obtain the width and height of an image tensor that is produced by tf.image.decode_jpeg?

asked 2021-08-22 11:00:00 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-12-20 14:00:00 +0000

lalupa gravatar image

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())
edit flag offensive delete link more

Your Answer

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

Add Answer


Question Tools

Stats

Asked: 2021-08-22 11:00:00 +0000

Seen: 13 times

Last updated: Dec 20 '22