1

The situation I am in now is that I have a lot of pictures with different sizes, some are 769x864 pixels others are 2000x3123 pixels and some are even 3500x4000 pixels.
I want to resize all the pictures to a certain height and width or at least close to the given height and width using openCV.
I know the cv2.resize() function but the problem is how I would define a fixed dimension, for example 800x900, without it changing the original pictures' shape.

Does anyone have an idea how this can be done?

Nawin Narain
  • 353
  • 1
  • 4
  • 17
  • Compute the scale factor for fx or fy using one of the dimensions of the output/input. Then use that factor for both fx and fy. The result will match one dimension and keep the aspect ratio of the input. For example fx=fy=output_width/input_width or fx=fy=output_height/input_height. If you need the result to be exactly 800x900, then you have to either pad the result or crop the result. – fmw42 Jan 04 '20 at 18:39
  • 1
    You could, possibly, be looking for *"Seam Carving"*, also known as *"Liquid Rescaling"*, see https://en.m.wikipedia.org/wiki/Seam_carving – Mark Setchell Jan 04 '20 at 23:00
  • https://stackoverflow.com/questions/43391205/add-padding-to-images-to-get-them-into-the-same-shape – Tor Thanawat Jan 07 '22 at 08:56

2 Answers2

1

That is possible with padding.

If your target size is 800*900, that means the images must have the same ratio (8/9) to be resized without shape changing.

For example if the original image has this size: 2400 * 2600, you should pad the columns with 100 pixels (e.g. add 100 zero pixels to each row border) to make the image 2400*2700. Then you can resize the result image to 800*900 (with 1/3 scale factor) without any change in the shapes of the image.

MeiH
  • 1,763
  • 11
  • 17
0

If I understood you correctly you want to scale down your picture. You can do it like that:

cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])

you can find more details and demo code over here: https://www.tutorialkart.com/opencv/python/opencv-python-resize-image/

Oded BD
  • 2,788
  • 27
  • 30