望熙貴
望熙貴

Reputation: 25

how to make two different image size equally

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?

Upvotes: 2

Views: 17882

Answers (4)

DaReal
DaReal

Reputation: 637

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')

Upvotes: 0

Muhammad Hammad Ahmad
Muhammad Hammad Ahmad

Reputation: 89

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))

Upvotes: 2

Thomas Weglinski
Thomas Weglinski

Reputation: 1094

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.

Upvotes: 0

J. A. Streich
J. A. Streich

Reputation: 1712

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:

Upvotes: 0

Related Questions