Skip to content
Advertisement

OpenCV Python: How to overlay an image into the centre of another image

How can I paste a smaller image into the center of another image? Both images have the same height, but the smaller one has a width that is always smaller.

The resulting image should be the smaller image with black bars around it so it is square.

JavaScript

resizedImg

resizedImg

blankImg

blankImg

Advertisement

Answer

Here is one way. You compute the offsets in x and y for the top left corner of the resized image where it would be when the resized image is centered in the background image. Then use numpy indexing to place the resized image in the center of the background.

JavaScript


User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement