2

I am trying to overlay an image onto another image, similar to a watermark. It works on a test logo, but when I try it with the actual logo, I get the error:

Traceback (most recent call last):
  File "C:/Users/dv/PycharmProjects/Image/main.py", line 28, in <module>
    result = cv2.addWeighted(roi, 1, logo, 0.3, 0)
cv2.error: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-_8k9tw8n\opencv\modules\core\src\arithm.cpp:650: error: (-209:Sizes of input arguments do not match) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function 'cv::arithm_op'

The size looks the same, so I'm not sure what it's referring to. For example, if I run the code using this logo,

logo

Onto the image:

image

It shows up fine:

output

But when I use this logo:

error logo

It gave the above error.

Code:

import cv2
import numpy as np
import glob
import os

logo = cv2.imread("mylogo.png")
h_logo, w_logo, _ = logo.shape

images_path = glob.glob("images/*.*")

print("Adding watermark")
for img_path in images_path:
    img = cv2.imread(img_path)
    h_img, w_img, _ = img.shape

    # Get the center of the original. It's the location where we will place the watermark
    center_y = int(h_img / 2)
    center_x = int(w_img / 2)
    top_y = center_y - int(h_logo / 2)
    left_x = center_x - int(w_logo / 2)
    bottom_y = top_y + h_logo
    right_x = left_x + w_logo

    # Get ROI
    roi = img[top_y: bottom_y, left_x: right_x]

    # Add the Logo to the Roi
    result = cv2.addWeighted(roi, 1, logo, 0.3, 0)

    # Replace the ROI on the image
    img[top_y: bottom_y, left_x: right_x] = result

    # Get filename and save the image
    filename = os.path.basename(img_path)
    cv2.imwrite("images/watermarked_" + filename, img)


print("Watermark added to all the images")
Michael S.
  • 3,050
  • 4
  • 19
  • 34
bonijad383
  • 109
  • 3
  • 7
  • Please show the full error stack. [ask] – Julien Jun 15 '21 at 05:32
  • @Julien What do you mean by that? I have included all the error messages – bonijad383 Jun 15 '21 at 05:33
  • The error stack is all the nested calls leading to the final error, including which original line in your own code causing the error. https://en.wikipedia.org/wiki/Stack_trace – Julien Jun 15 '21 at 05:35
  • 1
    @Julien I'm not sure what I can add on further, that's all I have – bonijad383 Jun 15 '21 at 05:42
  • That's pretty much it! – Julien Jun 15 '21 at 06:21
  • Seems to be a size issue (i.e. trying to overlay a logo that's bigger than the original image), check the boundary values of your slices... If it's not the issue then please create a [mre] – Julien Jun 15 '21 at 06:27
  • Does this answer your question? [Using openCV to overlay transparent image onto another image](https://stackoverflow.com/questions/40895785/using-opencv-to-overlay-transparent-image-onto-another-image) – Christoph Rackwitz Sep 09 '22 at 10:54

1 Answers1

0

After loading the logo check its size. It seems your second logo is much bigger than your background image.

  • First logo's size: 600x156x3
  • Background picture: 1280x1920x3

So when you get the roi:

top_y = center_y - int(h_logo / 2) # 960 - 78 = 782
left_x = center_x - int(w_logo / 2) # 640 - 300 = 340

bottom_y = top_y + h_logo # 782 + 156 = 938
right_x = left_x + w_logo # 340 + 600 = 940

# Get ROI
roi = img[top_y: bottom_y, left_x: right_x]
# roi = img[782:938, 340:940] this is okay because img.shape=(1920, 1280, 3)

However when you try to put another logo there is an error with the shape of logo:

  • Second logo's size: 3544x3544x3
  • Background picture: 1280x1920x3
top_y = center_y - int(h_logo / 2) # 960 - 1772 = -812
left_x = center_x - int(w_logo / 2) # 640 - 1772 = -1132
# from these two points it's clear that coordinate cannot be negative so you should resize the second logo

bottom_y = top_y + h_logo 
right_x = left_x + w_logo

# Get ROI
roi = img[top_y: bottom_y, left_x: right_x]
# roi = img[782:938, 340:940] this is okay because img.shape=(1920, 1280, 3)

So, you should resize the second logo after loading it. you may put something similar to this:

logo = cv2.imread("secondLogo.png")
logo = cv2.resize(logo, (156, 600)
h_logo, w_logo, _ = logo.shape
yakhyo
  • 1,540
  • 1
  • 6
  • 22