本文目录导读:

我来帮你写一个图片加水印的脚本,包含多种实现方式:
Python PIL 版本(推荐)
from PIL import Image, ImageDraw, ImageFont
import os
def add_watermark(input_path, output_path, watermark_text, position='bottom-right'):
"""
为图片添加文字水印
参数:
input_path: 输入图片路径
output_path: 输出图片路径
watermark_text: 水印文字
position: 水印位置 ('top-left', 'top-right', 'bottom-left', 'bottom-right', 'center')
"""
# 打开图片
img = Image.open(input_path).convert('RGBA')
# 创建水印图层
watermark = Image.new('RGBA', img.size, (0, 0, 0, 0))
draw = ImageDraw.Draw(watermark)
# 设置字体(如果系统中没有Arial,可以使用默认字体)
try:
font = ImageFont.truetype('Arial.ttf', 36)
except:
font = ImageFont.load_default()
# 获取文字尺寸
text_bbox = draw.textbbox((0, 0), watermark_text, font=font)
text_width = text_bbox[2] - text_bbox[0]
text_height = text_bbox[3] - text_bbox[1]
# 计算水印位置
margin = 20
positions = {
'top-left': (margin, margin),
'top-right': (img.width - text_width - margin, margin),
'bottom-left': (margin, img.height - text_height - margin),
'bottom-right': (img.width - text_width - margin, img.height - text_height - margin),
'center': ((img.width - text_width) // 2, (img.height - text_height) // 2)
}
position_coords = positions.get(position, positions['bottom-right'])
# 绘制水印(半透明)
draw.text(position_coords, watermark_text, font=font, fill=(255, 255, 255, 128))
# 合并图片
watermarked = Image.alpha_composite(img, watermark)
# 保存结果
watermarked = watermarked.convert('RGB')
watermarked.save(output_path, quality=95)
def batch_watermark(folder_path, watermark_text):
"""
批量添加水印
"""
supported_formats = ('.jpg', '.jpeg', '.png', '.bmp', '.tiff')
# 创建输出文件夹
output_folder = os.path.join(folder_path, 'watermarked')
os.makedirs(output_folder, exist_ok=True)
# 遍历文件夹中的所有图片
for filename in os.listdir(folder_path):
if filename.lower().endswith(supported_formats):
input_path = os.path.join(folder_path, filename)
output_path = os.path.join(output_folder, filename)
try:
add_watermark(input_path, output_path, watermark_text)
print(f"已处理: {filename}")
except Exception as e:
print(f"处理 {filename} 时出错: {e}")
# 使用示例
if __name__ == "__main__":
# 单张图片
add_watermark('input.jpg', 'output.jpg', '© 2024 My Watermark')
# 批量处理
batch_watermark('./images', '© 2024 My Watermark')
图片/Logo水印版本
from PIL import Image
import os
def add_logo_watermark(input_path, output_path, logo_path, opacity=0.5, scale=0.2):
"""
添加Logo/图片水印
"""
# 打开图片
img = Image.open(input_path).convert('RGBA')
# 打开Logo
logo = Image.open(logo_path).convert('RGBA')
# 缩放Logo
logo_width = int(img.width * scale)
logo_height = int(logo.height * (logo_width / logo.width))
logo = logo.resize((logo_width, logo_height), Image.Resampling.LANCZOS)
# 调整不透明度
if opacity < 1:
logo = logo.copy()
logo.putalpha(int(255 * opacity))
# 计算位置(右下角)
margin = 20
position = (img.width - logo_width - margin, img.height - logo_height - margin)
# 粘贴Logo
img.paste(logo, position, logo)
# 保存
img.convert('RGB').save(output_path, quality=95)
# 使用示例
# add_logo_watermark('input.jpg', 'output.jpg', 'logo.png')
平铺水印版本
from PIL import Image, ImageDraw, ImageFont
import math
def add_tiled_watermark(input_path, output_path, watermark_text):
"""
添加平铺水印
"""
img = Image.open(input_path).convert('RGBA')
# 创建水印图层
watermark = Image.new('RGBA', img.size, (0, 0, 0, 0))
draw = ImageDraw.Draw(watermark)
# 设置字体
try:
font = ImageFont.truetype('Arial.ttf', 24)
except:
font = ImageFont.load_default()
# 旋转水印
text_width = len(watermark_text) * 15
text_height = 30
step = 200 # 水印间距
# 平铺绘制水印
for y in range(-step, img.height + step, step):
for x in range(-step, img.width + step, step):
# 交替偏移
offset_x = x + (y // step % 2) * (step // 2)
draw.text((offset_x, y), watermark_text, font=font, fill=(200, 200, 200, 60))
# 合并图片
watermarked = Image.alpha_composite(img, watermark)
watermarked.convert('RGB').save(output_path, quality=95)
命令行版本
创建一个简单的命令行工具:
#!/usr/bin/env python3
# watermark_cli.py
import argparse
from PIL import Image, ImageDraw, ImageFont
import os
def main():
parser = argparse.ArgumentParser(description='图片加水印工具')
parser.add_argument('input', help='输入图片或文件夹路径')
parser.add_argument('text', help='水印文字')
parser.add_argument('-o', '--output', default='output', help='输出路径')
parser.add_argument('-p', '--position', default='bottom-right',
choices=['top-left', 'top-right', 'bottom-left', 'bottom-right', 'center'],
help='水印位置')
parser.add_argument('--batch', action='store_true', help='批量处理模式')
args = parser.parse_args()
if args.batch:
# 批量处理
if os.path.isdir(args.input):
batch_watermark(args.input, args.text)
else:
print("批量模式需要指定文件夹路径")
else:
# 单张处理
add_watermark(args.input, args.output, args.text, args.position)
if __name__ == "__main__":
main()
安装依赖
pip install Pillow
使用示例
# 单张图片 python watermark.py input.jpg "© 2024" -o output.jpg # 批量处理 python watermark.py ./images "© 2024" --batch # 指定位置 python watermark.py input.jpg "水印" -p center
选择哪种方式取决于你的需求:
- 简单文字水印:使用第一个版本
- Logo水印:使用第二个版本
- 防盗图:使用第三个平铺版本
- 命令行工具:使用第四个版本
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播