0

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.

ofirule
  • 4,233
  • 2
  • 26
  • 40
임승민
  • 1
  • 1
  • 1
    is your img RGB or grayscale? – Vishesh Mangla Aug 04 '20 at 14:02
  • it's grayscale image – 임승민 Aug 04 '20 at 14:26
  • I don't think grayscale image has channels. That seems wrong there – Vishesh Mangla Aug 04 '20 at 14:44
  • `image.paste()` works only with `pillow.Image` but `cv2` gives you `numpy.array` so you can do `image[row_start:row_end,col_start:col_end] = other_image` and you don't have to use `[c]` – furas Aug 04 '20 at 15:53
  • 1
    if you have greyscale images then you should use only `[y1:y2, x1:x2]` without `[c]` – furas Aug 04 '20 at 15:54
  • always put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot). There are other useful information. – furas Aug 04 '20 at 16:11
  • if you have `img` with `RGBA` and `img_overlay` with `RGB` then you may have problem because first has 4 channels `0,1,2,3` but second has only 3 channels `0,1,2` and it has no `index 3` -like in your error `"index 3 is out of bounds"` – furas Aug 04 '20 at 16:13
  • check this https://stackoverflow.com/questions/56002672/display-an-image-over-another-image-at-a-particular-co-ordinates-in-opencv/56004185#56004185 – user8190410 Aug 05 '20 at 07:49
  • Is that code correct for rgb scale images? – 임승민 Aug 05 '20 at 10:57

0 Answers0