📜  使用 OpenCV 和Python图像进行水印

📅  最后修改于: 2022-05-13 01:55:31.892000             🧑  作者: Mango

使用 OpenCV 和Python图像进行水印

在本文中,我们将看到如何在Python使用 OpenCV 制作水印图像。

水印有意在图像上留下文本/标志。艺术家通常使用水印来保护图像的版权。使用水印我们可以确保图像的所有者是在图像上印制水印的人。

让我们借助一个例子来理解这一点,图像上的水印是什么意思:

水印前的图片

标识:

标志.jpg

分步实施:

步骤 1:导入 OpenCV 并阅读徽标和要应用水印的图像。



Python3
# watermarking image using OpenCV
  
# importing cv2
import cv2
  
# importing logo that we are going to use
logo = cv2.imread("logo.jpg")
  
# importing image on which we are going to 
# apply watermark
img = cv2.imread("dark.png")


Python3
# calculating dimensions
# height and width of the logo
h_logo, w_logo, _ = logo.shape
  
# height and width of the image
h_img, w_img, _ = img.shape


Python3
# calculating coordinates of center
# calculating center, where we are going 
# to place our watermark
center_y = int(h_img/2)
center_x = int(w_img/2)
  
# calculating from top, bottom, right and left
top_y = center_y - int(h_logo/2)
bottom_y = top_y + h_logo
right_x = left_x + w_logo
left_x = center_x - int(w_logo/2)


Python3
# adding watermark to the image
destination = img[top_y:bottom_y, left_x:right_x]
result = cv2.addWeighted(destination,1, logo, 0.5, 0)


Python3
# displaying and saving image
img[top_y:bottom_y, left_x:right_x] = result
cv2.imwrite("watermarked.jpg", img)
cv2.imshow("Watermarked Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()


Python3
# watermarking image using OpenCV
  
# importing cv2
import cv2
  
# loading images
# importing logo that we are going to use
logo = cv2.imread("logo.jpg")
  
# importing image on which we are going to
# apply watermark
img = cv2.imread("dark.png")
  
# calculating dimensions
# height and width of the logo
h_logo, w_logo, _ = logo.shape
  
# height and width of the image
h_img, w_img, _ = img.shape
  
# calculating coordinates of center
# calculating center, where we are going to 
# place our watermark
center_y = int(h_img/2)
center_x = int(w_img/2)
  
# calculating from top, bottom, right and left
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
  
# adding watermark to the image
destination = img[top_y:bottom_y, left_x:right_x]
result = cv2.addWeighted(destination, 1, logo, 0.5, 0)
  
# displaying and saving image
img[top_y:bottom_y, left_x:right_x] = result
cv2.imwrite("watermarked.jpg", img)
cv2.imshow("Watermarked Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()


步骤2:计算两个图像的高度和宽度,并将它们保存到其他变量中。我们需要计算宽度和高度,因为我们要将水印放置在图像上的某个位置,为此,我们只需要知道徽标和图像的正确宽度和高度。

蟒蛇3

# calculating dimensions
# height and width of the logo
h_logo, w_logo, _ = logo.shape
  
# height and width of the image
h_img, w_img, _ = img.shape

在这里,我们使用了 OpenCV 中的shape函数,它返回图像的高度和宽度的元组。

第 3 步:现在,我们将计算图像中心的坐标,因为我们要将水印放置在图像的中心。

蟒蛇3

# calculating coordinates of center
# calculating center, where we are going 
# to place our watermark
center_y = int(h_img/2)
center_x = int(w_img/2)
  
# calculating from top, bottom, right and left
top_y = center_y - int(h_logo/2)
bottom_y = top_y + h_logo
right_x = left_x + w_logo
left_x = center_x - int(w_logo/2)

第 4 步:要为图像添加水印,我们将使用 OpenCV 中的 addWeighted函数。首先,我们将提供要放置水印的目的地,然后将该目的地传递给带有图像和徽标的 addWeighted函数。

在我们的例子中,源 1 将是我们想要放置徽标的图像,alpha 将是徽标的不透明度,源 2 将是徽标本身,我们将相应地设置 beta,即不透明度的 alpha 和伽马将为 0。

蟒蛇3

# adding watermark to the image
destination = img[top_y:bottom_y, left_x:right_x]
result = cv2.addWeighted(destination,1, logo, 0.5, 0)

第 5 步:之后,我们只是显示结果并保存输出。为了显示我们使用imshow函数的输出并写入/保存图像,我们在两个函数中都使用imwrite函数,首先我们必须提供文件名作为参数,然后是文件本身。 cv2.waitKey(0) 用于等待直到用户按下 Esc 键,之后 cv2.destroyAllWindows函数将关闭窗口。

蟒蛇3

# displaying and saving image
img[top_y:bottom_y, left_x:right_x] = result
cv2.imwrite("watermarked.jpg", img)
cv2.imshow("Watermarked Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

下面是完整的实现:

蟒蛇3

# watermarking image using OpenCV
  
# importing cv2
import cv2
  
# loading images
# importing logo that we are going to use
logo = cv2.imread("logo.jpg")
  
# importing image on which we are going to
# apply watermark
img = cv2.imread("dark.png")
  
# calculating dimensions
# height and width of the logo
h_logo, w_logo, _ = logo.shape
  
# height and width of the image
h_img, w_img, _ = img.shape
  
# calculating coordinates of center
# calculating center, where we are going to 
# place our watermark
center_y = int(h_img/2)
center_x = int(w_img/2)
  
# calculating from top, bottom, right and left
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
  
# adding watermark to the image
destination = img[top_y:bottom_y, left_x:right_x]
result = cv2.addWeighted(destination, 1, logo, 0.5, 0)
  
# displaying and saving image
img[top_y:bottom_y, left_x:right_x] = result
cv2.imwrite("watermarked.jpg", img)
cv2.imshow("Watermarked Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

输出:

加水印后