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,
Onto the image:
It shows up fine:
But when I use this 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")