I'm trying to paste an image in another image(larger one). I know there is a way using image.paste() function, but it seems that it doesn't work for images read by cv.imread, but works only for images opened by Image.open() function. And I'd also search a code for pasting and image in opencv which is the code below.
def overlay_image_alpha(img, img_overlay, pos, alpha_mask):
"""Overlay img_overlay on top of img at the position specified by
pos and blend using alpha_mask.
Alpha mask must contain values within the range [0, 1] and be the
same size as img_overlay.
"""
x, y = pos
# Image ranges
y1, y2 = max(0, y), min(img.shape[0], y + img_overlay.shape[0])
x1, x2 = max(0, x), min(img.shape[1], x + img_overlay.shape[1])
# Overlay ranges
y1o, y2o = max(0, -y), min(img_overlay.shape[0], img.shape[0] - y)
x1o, x2o = max(0, -x), min(img_overlay.shape[1], img.shape[1] - x)
# Exit if nothing to do
if y1 >= y2 or x1 >= x2 or y1o >= y2o or x1o >= x2o:
return
channels = img.shape[2]
alpha = alpha_mask[y1o:y2o, x1o:x2o]
alpha_inv = 1.0 - alpha
for c in range(channels):
img[y1:y2, x1:x2, c] = (alpha * img_overlay[y1o:y2o, x1o:x2o, c] +
alpha_inv * img[y1:y2, x1:x2, c])
but i end up getting "IndexError: index 3 is out of bounds for axis 2 with size 3" error. brief explanation for what i need to do is
cv2.imread("largerimagepath")
cv2.imread("smallerimagepath")
##paste smaller image at some point(x,y) in largerimage
It would be really nice if i can get a code for pasting image and some explanation for "IndexError: index 3 is out of bounds for axis 2 with size 3" error.