I'm new in OpenCV, and I will be happy to help me
I have a transparent watermark image like this
And I want to put watermark on bottom-left corner of multiple images with python OpenCV
each images have difference size
and before put I want to resize the watermark to fit the size of the image, that the logo should not be scaled down or up
something like this image:
and here is my code:
import cv2
img1 = cv2.imread('my_image.png')
img2 = cv2.imread('my_watermark.png')
h, w = img1.shape[:2]
rows,cols,channels = img2.shape
roi = img1[0:rows, 0:cols]
img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 220, 255, cv2.THRESH_BINARY_INV)
mask_inv = cv2.bitwise_not(mask)
img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)
img2_fg = cv2.bitwise_and(img2,img2,mask = mask)
dst = cv2.add(img1_bg,img2_fg)
img1[0:rows, 0: cols] = dst
cv2.imwrite('imglogo.png', img1)
but I have two problems with this code
first, the watermark is located to the top-right on image
Secondly, the watermark loses its transparency
and the image becomes like this
I try this:
image = cv2.imread('my_image.png')
watermark = cv2.imread('my_watermark.png', cv2.IMREAD_UNCHANGED)
(wH, wW) = watermark.shape[:2]
weight = 1 - watermark[:, :, 3] / 255
num1 = 1250
num2 = 50
image[num1:num1 + wH, num2:num2 + wW, 0] = np.multiply(image[num1:num1 + wH, num2:num2 + wW, 0], weight).astype(np.uint8)
image[num1:num1 + wH, num2:num2 + wW, 1] = np.multiply(image[num1:num1 + wH, num2:num2 + wW, 1], weight).astype(np.uint8)
image[num1:num1 + wH, num2:num2 + wW, 2] = np.multiply(image[num1:num1 + wH, num2:num2 + wW, 2], weight).astype(np.uint8)
output = cv2.addWeighted(image[num1:num1 + wH, num2:num2 + wW], 1, watermark[:, :, 0:3], 1, 1)
image[num1:num1 + wH, num2:num2 + wW] = output
cv2.imwrite("watermark3.png", image)
but in different images, the size of each image changes
so, num1 and num2 must be changed
I was a little confused, how can I put watermark on bottom-left corner on multiple images?