使用Python给批量图片加水印

可以调整水印位置,旋转,缩放大小等参数,后加了控制水印透明度

基于前文,修改了代码bug,增加了水印处于图像边缘,超过画布时的提醒

import os
import cv2
import numpy as np

# 旋转图像
def rotate_image(image, angle):
    # ... existing code ...
    # 计算旋转后的图像尺寸
    rows, cols = image.shape[:2]
    rotation_matrix = cv2.getRotationMatrix2D((cols / 2, rows / 2), angle, 1.0)
    cos = np.abs(rotation_matrix[0, 0])
    sin = np.abs(rotation_matrix[0, 1])
    new_width = int((rows * sin) + (cols * cos))
    new_height = int((rows * cos) + (cols * sin))
    # 计算平移矩阵,确保旋转后图像不被裁切
    rotation_matrix[0, 2] += (new_width / 2) - cols / 2
    rotation_matrix[1, 2] += (new_height / 2) - rows / 2
    # 应用旋转矩阵
    rotated_image = cv2.warpAffine(image, rotation_matrix, (new_width, new_height), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(255, 255, 255, 0))
    return rotated_image

def add_watermark_to_folder(input_folder, output_folder, watermark_path, scale_factor=20, x_position_factor=2/3, y_position_factor=2/3, rotation_angle=0):
    # 确保输出文件夹存在
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    # 获取水印图
    watermark_image = cv2.imread(watermark_path, cv2.IMREAD_UNCHANGED)
    original_height, original_width = watermark_image.shape[:2]

    # 旋转水印图
    rotated_watermark = rotate_image(watermark_image, rotation_angle)

    # 计算旋转后的水印图的新尺寸
    rotated_height, rotated_width = rotated_watermark.shape[:2]

    # 扩展图像大小以容纳整个旋转后的水印
    new_height = max(original_height, rotated_height)
    new_width = max(original_width, rotated_width)

    # 循环处理文件夹中的所有图像
    for filename in os.listdir(input_folder):
        if filename.endswith(('.jpg', '.bmp', '.png')):
            input_path = os.path.join(input_folder, filename)
            output_path = os.path.join(output_folder, filename)

            add_watermark_to_image(input_path, output_path, rotated_watermark, scale_factor, x_position_factor, y_position_factor)

def add_watermark_to_image(input_path, output_path, watermark_image, scale_factor, x_position_factor, y_position_factor):
    # 读取原始图
    source_image = cv2.imread(input_path)

    # 计算水印图的新尺寸,使其面积为原始图的 scale_factor 倍
    original_height, original_width = source_image.shape[:2]
    new_area = original_width * original_height / scale_factor
    scale_factor = (new_area / watermark_image.shape[0] / watermark_image.shape[1]) ** 0.5
    new_width = int(watermark_image.shape[1] * scale_factor)
    new_height = int(watermark_image.shape[0] * scale_factor)

    # 等比缩放水印图
    watermark_resized = cv2.resize(watermark_image, (new_width, new_height), interpolation=cv2.INTER_AREA)

    # 计算水印的位置
    x_position = int(original_width * x_position_factor - new_width / 2)
    y_position = int(original_height * y_position_factor - new_height / 2)

    # 确保水印图不会超出原始图边界
    if x_position < 0 or y_position < 0 or x_position + new_width > original_width or y_position + new_height > original_height:
        print("Error: Watermark position is too close to or outside the edge of the source image.")
        return  # 结束函数

    # 获取水印图的透明通道
    alpha_channel = watermark_resized[:, :, 3] / 255.0

    # 将水印图覆盖在原始图上
    for c in range(0, 3):
        source_image[y_position:y_position+new_height, x_position:x_position+new_width, c] = \
            source_image[y_position:y_position+new_height, x_position:x_position+new_width, c] * (1 - alpha_channel) + \
            watermark_resized[:, :, c] * alpha_channel

    # 保存合并后的图像
    # 打印文件名
    print("Processing:", input_path)
    cv2.imwrite(output_path, source_image)

if __name__ == "__main__":
    # 使用示例,指定参数
    input_folder = './input'
    output_folder = './output'
    watermark_path = 'water.png'
    scale_factor = 100000  # scale_factor > 10
    rotation_angle = 90  # 旋转角度
    x_position_factor = 1/2
    y_position_factor = 1/2

    add_watermark_to_folder(input_folder, output_folder, watermark_path, scale_factor, x_position_factor, y_position_factor, rotation_angle)

在原来的基础上增加了控制水印透明度的参数

import os
import cv2
import numpy as np

# 旋转图像
def rotate_image(image, angle):
    # 计算旋转后的图像尺寸
    rows, cols = image.shape[:2]
    rotation_matrix = cv2.getRotationMatrix2D((cols / 2, rows / 2), angle, 1.0)
    cos = np.abs(rotation_matrix[0, 0])
    sin = np.abs(rotation_matrix[0, 1])
    new_width = int((rows * sin) + (cols * cos))
    new_height = int((rows * cos) + (cols * sin))
    # 计算平移矩阵,确保旋转后图像不被裁切
    rotation_matrix[0, 2] += (new_width / 2) - cols / 2
    rotation_matrix[1, 2] += (new_height / 2) - rows / 2
    # 应用旋转矩阵
    rotated_image = cv2.warpAffine(image, rotation_matrix, (new_width, new_height), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(255, 255, 255, 0))
    return rotated_image

def add_watermark_to_folder(input_folder, output_folder, watermark_path, scale_factor=20, x_position_factor=2/3, y_position_factor=2/3, rotation_angle=0, watermark_opacity=1.0):
    # 确保输出文件夹存在
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    # 获取水印图
    watermark_image = cv2.imread(watermark_path, cv2.IMREAD_UNCHANGED)
    original_height, original_width = watermark_image.shape[:2]

    # 旋转水印图
    rotated_watermark = rotate_image(watermark_image, rotation_angle)

    # 计算旋转后的水印图的新尺寸
    rotated_height, rotated_width = rotated_watermark.shape[:2]

    # 扩展图像大小以容纳整个旋转后的水印
    new_height = max(original_height, rotated_height)
    new_width = max(original_width, rotated_width)

    # 循环处理文件夹中的所有图像
    for filename in os.listdir(input_folder):
        if filename.endswith(('.jpg', '.bmp', '.png')):
            input_path = os.path.join(input_folder, filename)
            output_path = os.path.join(output_folder, filename)

            add_watermark_to_image(input_path, output_path, rotated_watermark, scale_factor, x_position_factor, y_position_factor, watermark_opacity)

def add_watermark_to_image(input_path, output_path, watermark_image, scale_factor, x_position_factor, y_position_factor, watermark_opacity=1.0):
    # 读取原始图
    source_image = cv2.imread(input_path)

    # 计算水印图的新尺寸,使其面积为原始图的 scale_factor 倍
    original_height, original_width = source_image.shape[:2]
    new_area = original_width * original_height / scale_factor
    scale_factor = (new_area / watermark_image.shape[0] / watermark_image.shape[1]) ** 0.5
    new_width = int(watermark_image.shape[1] * scale_factor)
    new_height = int(watermark_image.shape[0] * scale_factor)

    # 等比缩放水印图
    watermark_resized = cv2.resize(watermark_image, (new_width, new_height), interpolation=cv2.INTER_AREA)

    # 计算水印的位置
    x_position = int(original_width * x_position_factor - new_width / 2)
    y_position = int(original_height * y_position_factor - new_height / 2)

    # 确保水印图不会超出原始图边界
    if x_position < 0 or y_position < 0 or x_position + new_width > original_width or y_position + new_height > original_height:
        print("Error: Watermark position is too close to or outside the edge of the source image.")
        return  # 结束函数

    # 获取水印图的透明通道
    alpha_channel = watermark_resized[:, :, 3] / 255.0

    # 将水印图覆盖在原始图上
    for c in range(0, 3):
        source_image[y_position:y_position + new_height, x_position:x_position + new_width, c] = \
            source_image[y_position:y_position + new_height, x_position:x_position + new_width, c] * (
                        1 - alpha_channel * watermark_opacity) + \
            watermark_resized[:, :, c] * alpha_channel * watermark_opacity

    # 保存合并后的图像
    # 打印文件名
    print("Processing:", input_path)
    cv2.imwrite(output_path, source_image)

if __name__ == "__main__":
    # 使用示例,指定参数
    input_folder = './input'
    output_folder = './output2'
    watermark_path = 'logo.png'
    scale_factor = 200  # scale_factor > 10
    rotation_angle = 0  # 旋转角度
    x_position_factor = 1/2
    y_position_factor = 3/4
    watermark_opacity = 0.9  # 设置透明度,取值范围为[0, 1],1表示完全不透明,0表示完全透明
    add_watermark_to_folder(input_folder, output_folder, watermark_path, scale_factor, x_position_factor, y_position_factor, rotation_angle, watermark_opacity)

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值