使用Python批量给一个文件夹中的图片加水印

准备好水印图片,以及输入文件夹,输出文件夹,可以控制水印的旋转,缩放,位置

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)
    rotated_image = cv2.warpAffine(image, rotation_matrix, (cols, rows), 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)
    watermark_image = rotate_image(watermark_image, rotation_angle)

    # 循环处理文件夹中的所有图像
    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, watermark_image, 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)

    # 获取水印图的透明通道
    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 = 'Watermark02.png'
    scale_factor = 40  # scale_factor > 10
    rotation_angle = 90
    x_position_factor = 1/2
    y_position_factor = 2/3

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

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Python的os模块和shutil模块来批量替换多个文件夹图片文件名称的某个字符。 具体步骤如下: 1. 导入os模块和shutil模块 ```python import os import shutil ``` 2. 遍历指定目录下的所有文件夹 ```python root_path = './images' # 指定根目录 for root, dirs, files in os.walk(root_path): for filename in files: filepath = os.path.join(root, filename) # 获取文件路径 # 对文件进行操作 ``` 3. 对图片文件进行操作 根据文件扩展名来判断是否为图片文件,然后使用字符串替换函数replace()替换文件名称的特定字符。例如,将所有文件名称的字符"old"替换成"new"。 ```python if filename.endswith('.jpg') or filename.endswith('.png'): # 判断是否为图片文件 new_filename = filename.replace('old', 'new') # 替换字符 new_filepath = os.path.join(root, new_filename) # 新文件路径 os.rename(filepath, new_filepath) # 修改文件名称 ``` 完整代码示例: ```python import os import shutil root_path = './images' # 指定根目录 for root, dirs, files in os.walk(root_path): for filename in files: filepath = os.path.join(root, filename) # 获取文件路径 if filename.endswith('.jpg') or filename.endswith('.png'): # 判断是否为图片文件 new_filename = filename.replace('old', 'new') # 替换字符 new_filepath = os.path.join(root, new_filename) # 新文件路径 os.rename(filepath, new_filepath) # 修改文件名称 ``` 这样就可以批量替换多个文件夹图片文件名称的某个字符了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值