How to pad image in tensorflow?

The original image is in shape (720, 1280) and contains a bounding box. I need to make it square without disturbing the spatial information. Please look at the below picture.

Demonstration image.

I’m not sure how to add such up-down padding to this image to make it a square shape. The width is 1280 and the height is 720. So, to make it square I need to add a border of shape (1280-720) = 560 - divided by 2 - 280 px up and down. How to do this in tensorflow efficiently and dynamically?

use tf.pad with mode ‘CONSTANT’ should work.

I tried but couldn’t make it work. :frowning:

if your Image is a tensor with shape (720, 1280,3) ( 3 channels for RGB image) then it should work with paddings = tf.constant([[280, 280], [0, 0], [0,0]])


Thanks. I did some syntax mistakes. It’s working but what about the bounding box information. How to scale that?

do you mean the blue squares? what about them? do you need their new positions?

Yes. I mean I also need to rescale the bounding box to locate the target object, right?

Here is similar qn on so but didn’t looks ok .

lets say it is the x-axis where you add two times 280 pixels. then the x-coordinates of your bounding boxes has to be increased by 280 pixels. as the original image is not distorted, you dont have to change the size of the bounding boxes. just their position


This should be possible with tf.image.resize_with_pad.

It expects a 3D or 4D input, so I think your case you would want to do

tf.image.resize_with_pad(tf.expand_dims(image, -1), 1280, 1280)

@noaddy what about bbox re-scaling? Is there any built-in function for that?