我正在嘗試將水印粘貼到白色背景影像:
watermark = cv2.imread(watermark_path)
watermark_ratio = round(image.shape[1]/8) # calculating the ratio
resized_watermark = cv2.resize(watermark, (watermark_ratio, round(watermark_ratio/2)), interpolation = cv2.INTER_AREA) # resizing watermark size
h_logo, w_logo, _ = resized_watermark.shape
h_img, w_img, _ = image.shape
top_y = h_img - h_logo
left_x = w_img - w_logo
bottom_y = h_img
right_x = w_img
destination = image[top_y:bottom_y, left_x:right_x]
result = cv2.addWeighted(destination, 1, resized_watermark, 0.5, 0) # pasting watermark on original image
image[top_y:bottom_y, left_x:right_x] = result
cv2.imwrite(save_path, image) # saving watermarked the image
但是我在白色背景的影像上看不到水印,我想我必須使用addWeighted方法的引數。有時候是這樣的:
注意:水印貼在右下角。
水印
帶水印的影像(水印中的一些生命跡象)
帶水印的圖片#2
深色背景上帶有水印的影像
uj5u.com熱心網友回復:
該問題是由cv2.addWeighted, 在該行中的不正確使用引起的:
result = cv2.addWeighted(destination, 1, resized_watermark, 0.5, 0)
第二個引數(您設定為 1)是第一張影像的權重。
如果您希望影像和水印之間混合 50-50,則應將其更改為:
result = cv2.addWeighted(destination, 0.5, resized_watermark, 0.5, 0)
請參閱檔案:addWeighted.
還有一個使用示例:使用 OpenCV 添加(混合)兩個影像。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/477038.html
標籤:Python python-3.x opencv 水印
上一篇:如何從我的特定資料框中創建字典?
