2

Most of the function in OpenCV require src1 and src2 must be equal size. Does anyone know if I got two image file with different dimension, how to make the size equally?

望熙貴
  • 25
  • 1
  • 1
  • 4

4 Answers4

2
from PIL import Image

img = Image.open('frog.jpg')

img2 = Image.open('cat.jpg')

Nimg = img.resize((220,180))   # image resizing

Nimg2 = img2.resize((220,180))
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
0

You can scale them to match, typically by making the larger image smaller. The issue becomes that they have to have the same aspect ratio, and if they don't you'll have to crop the image without croping out the important part.

Scaling an image in opencv:

J. A. Streich
  • 1,683
  • 10
  • 13
0

Of course you can crop: https://stackoverflow.com/a/8268062/3076403 or resize the images: How to resize an image to a specific size in OpenCV?

But do you really need to do this? Please specify, what you want to achieve?

Most of the OpenCV functions processes one image to another doing a spatial pixel-to-pixel operations (e.g. filtering, morphological operations). So that it produces the output image in the same size as original and this is a desired behavior.

Community
  • 1
  • 1
Thomas Weglinski
  • 1,094
  • 1
  • 10
  • 21
0

2020 update: This library handles it really nicely, including expanding and cropping if needed: https://pypi.org/project/image-tools/

from PIL import Image
from image_tools.sizes import resize_and_crop

img_target_size = Image.open('img1.png')
img_to_resize = resize_and_crop(
    'img2.png', 
    (img_origin_size.size[0],img_origin_size.size[1]), #set width and height to match img1
    crop_origin="middle"
    )
output = img_to_resize.save('resized_img2.png')
DaReal
  • 597
  • 3
  • 10
  • 24