使用OpenCV与Python进行多图像水印处理
水印在今天的企业和在线内容信用中起着至关重要的作用。作者和公司所有者获得了数字人工制品的权利或信用,防止他们的内容在未经他们同意的情况下被盗用或复制。
水印可以作为一个独特的、独一无二的标志、签名或印章。
使用Photoshop或手动制作这些水印需要更多的努力和时间。我们使用OpenCV与Python使之比以往任何时候都更容易实现对多张图片的自动水印。
本文将尽可能多地介绍,使你了解OpenCV;一个用于自动制作图像水印和许多其他实际情况的python库。
前提条件
要完成本教程,你应该具备以下条件。
- 对Python和OpenCV库的基本概念有初步了解。
- 有能力使用OpenCV读取、写入和显示图像。
- 了解如何使用Pycharm IDE。
OpenCV库
OpenCV是一个方便的Python库,它是开源的,用于计算机视觉处理功能,以及做其他关键的操作,如跟踪摄像机的运动,提取任何物体的3D模型,创建图像过滤器和水印。
正如我们将在本教程中看到的那样,它是一个用于自动化图像水印的伟大工具。
我们可以通过以下方式安装该库。
使用Anaconda
Anaconda是一个免费的、开源的、用于科学计算的Python和R计算机语言的条件分布,使软件包的管理和部署更加容易。
成功安装Anaconda后,使用下面的Anaconda提示来安装OpenCV。
conda install -c conda-forge opencv
使用 pip install
对于Windows和Linux机器,你可以使用pip ,在终端或命令行上输入以下命令,在Windows上安装OpenCV。
pip install opencv-python
对于Mac设备,使用homebrew与下面的命令来安装OpenCV。
brew install opencv
安装完成后,我们将使用OpenCV进行多幅图像的水印处理,因为它更容易使用,而且用途更广。
这个方案将有两个文件夹:flowers ,其中包含要加水印的图像;watermarked_images ,存放加水印的图像。
其过程如下所示。

图像水印
给图像加水印可以用文字或图像来完成。在本教程中,我们将展示如何将图像水印应用于多个花卉图像。
下面的标志将使用OpenCV python库自动添加到图像上。

注意:我们使用一个黑色背景的白色标志。OpenCV库将黑色背景视为无背景,使标志清晰可见,并能很好地看到图像的某些部分。
导入库和加载标志图像
我们首先导入OpenCV库并加载logo图像以获得其高度和宽度。这些尺寸在放置水印时是需要的。
import cv2
import numpy as np
import glob
import os
from numpy._distributor_init import filename
logo = cv2.imread("elite_logo.jpg") #loading logo image
h_logo, w_logo, _ = logo.shape #getting height and width

获取要加水印的图像
一旦我们有了标志,我们就可以得到要加水印的图片的路径。这是用上面已经导入的glob 库完成的。
创建一个循环来加载所有的图像,当图像被定位时,获得它们的高度和宽度。这些图片位于flowers 文件夹中,如图所示。

images_path = glob.glob("flowers/*.*") #flowers is the folder holding the flower images to be watermarked.
print("Adding watermark")
for img_path in images_path: #loop loading the images and getting their width and height
img = cv2.imread(img_path)
h_img, w_img, _ = img.shape
放置水印
水印的位置取决于用户。可以把位置放在任何地方,但本文将把水印放在图片的中心位置。
我们将写一段代码来获得所有图片的中心,并使用cv2.addWeighted() 方法来指定标志的不透明度,以便在放置在图片上时看起来漂亮而平滑。
#get the image's center and the spot where the watermark should be placed
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
现在我们有了中心和位置,我们在图像上指定感兴趣的区域(ROI),水印将被放置在那里,然后将标志添加到图像上。
roi = img[top_y:bottom_y, left_x:right_x] #getting region of interest
result = cv2.addWeighted(roi, 1, logo, 0.3, 0) #adding logo to the ROI
img[top_y:bottom_y, left_x:right_x] = result #replacing ROI on the image
下图是一个水印花图像的输出样本。
这是原始花卉图像。
.
这是加了水印的图像。
.
保存加水印的图像
在加水印的过程中,我们得到图像的文件名,并将加水印的图像保存在一个不同的文件夹中,watermarked_images ,如图所示。
filename= os.path.basename(img_path)
cv2.imwrite("watermarked_images/watermarked_" + filename, img) #watermarked_images is the folder holding the watermarked images.
print("Watermark added to all images")
这里是完整的代码。
import cv2
import numpy as np
import glob
import os
from numpy._distributor_init import filename
logo = cv2.imread("elite_logo.jpg")
h_logo, w_logo, _ = logo.shape
images_path = glob.glob("flowers/*.*")
print("Adding watermark")
for img_path in images_path:
img = cv2.imread(img_path)
h_img, w_img, _ = img.shape
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
# Extracting the ROI
roi = img[top_y:bottom_y, left_x:right_x]
result = cv2.addWeighted(roi, 1, logo, 0.3, 0)
img[top_y:bottom_y, left_x:right_x] = result
filename= os.path.basename(img_path)
cv2.imwrite("watermarked_images/watermarked_" + filename, img)
print("Watermark added to all images")
当整个代码组合并运行成功后,它在命令行上显示两个打印函数的输出。
.
这个过程将导致每张分配有水印名称的图片与它的原始名称一起。
.
总结一下,我们已经。
- 了解了什么是OpenCV库以及它的应用。
- 探索了作为OpenCV应用之一的多种图像水印的演示。
- 用一个使用图像水印的小项目演示了图像水印。
- 学习了如何加载图像并获得图像的高度和宽度。
- 学习了如何定位图像的中心。
结论
正如我们所看到的,使用OpenCV库将水印应用到多个图像上是比较容易的,因为它是快速而有效的。
这使得一个组织有可能为其内容提供信用,而不必在每张图片上手动操作。