Python编程对比其它编程语言而言,主要的特点就是易学易用、跨平台、多功能。在上一节教程中,已经介绍了如何利用Python的Pillow库和Tkinter库轻松实现批量添加文字水印的功能。在本节教程中,将介绍如何使用Pillow库和Tkinter库批量给图片添加图片水印。
批量给图片添加图片水印的效果图如下所示:
1、批量给图片添加图片水印过程分析
我们的主要目标是批量为某个文件夹下的所有图片添加水印。首先,准备一张合适的水印图片(比如一个透明背景的logo图片),建议使用png支持透明通道的格式。同时,准备好需要添加水印的一批图片,将它们放在一个文件夹中,方便批量处理。
程序通过Python的PIL(Pillow)库和Tkinter图形用户界面(GUI)创建一个应用,用户可以通过简单的界面选择需要批量添加水印的文件夹和已准备好的水印图片,选择水印位置、透明度、水印缩放比例和水印角度,处理后的图片还能保存到指定的文件夹中。点击“批量添加水印”按钮后,程序会自动为指定文件夹中的所有图片添加图片水印,并保存到指定文件夹中。
在选择水印图片和调整水印参数过程中,左侧的预览区域会尝试打开指定文件夹内的第一个图像文件,显示出添加水印后的预览图片。
具体实现步骤如下:
-
首先,使用os.walk()方法遍历指定文件夹中的所有文件,筛选出图片文件(根据文件扩展名判断)。
-
使用Image.open()方法分别打开待处理图片和水印图片。
-
获取水印图片的尺寸、水印位置、透明度、缩放比例和角度,再使用paste()方法将水印图片粘贴到原图片上。
-
选择输出文件的路径,最后使用save()方法保存处理后的图片。
-
调用add_watermark_process()函数对每个图片进行水印添加操作。
2、代码实现
(1)安装Pillow库
首先,确保你已经安装了Pillow库。如果还没有安装,可以通过pip安装:
pip install pillow
(2) 导入必要的库
import os
import tkinter as tk
from tkinter import filedialog, messagebox, ttk
from PIL import Image, ImageTk
import time
import glob
(3)图片水印制作核心代码
# 图片水印制作函数
def add_watermark(open_image, watermark_path, output_folder_path, position, opacity, watermark_scale_percent):
try:
# 获取原始图片文件名(包含扩展名)
open_image_name = os.path.basename(open_image)
file_name, file_extension = os.path.splitext(open_image_name)
# 水印完成后,创建输出文件名,在原始文件名后添加_时间戳后缀
# 获取当前时间的时间元组
current_time = time.localtime()
# 将时间元组格式化为字符串,例如,将时间“2025年08月23日09时22分30秒”格式化为“20250823092230”
time_str = time.strftime("%Y%m%d%H%M%S", current_time)
output_image_name = file_name + "_" + time_str + file_extension
# 构建完整的输出文件路径
output_path = os.path.join(output_folder_path, output_image_name)
# 打开原始图片
image = Image.open(open_image)
# 获取原始图片宽度和高度
image_width, image_height = image.size
# 打开水印图片并确保其为RGBA模式
watermark_image = Image.open(watermark_path)
if watermark_image.mode!= 'RGBA':
watermark_image = watermark_image.convert('RGBA')
watermark_width, watermark_height = watermark_image.size
# 将百分比转换为小数
scale_percent = watermark_scale_percent / 100
new_watermark_width = int(watermark_width * scale_percent)
new_watermark_height = int(watermark_height * scale_percent)
resized_watermark = watermark_image.resize((new_watermark_width, new_watermark_height))
# 根据选择的位置调整水印位置
if position == "左上":
paste_position = (0, 0)
elif position == "右上":
paste_position = (image_width - new_watermark_width, 0)
elif position == "居中":
paste_position = (int((image_width - new_watermark_width)/2), int((image_height - new_watermark_height)/2))
elif position == "左下":
paste_position = (0, image_height - new_watermark_height)
elif position == "右下":
paste_position = (image_width - new_watermark_width, image_height - new_watermark_height)
else:
paste_position = (0, 0)
# 处理透明度
try:
if opacity < 100:
alpha = resized_watermark.split()[3]
alpha = alpha.point(lambda p: p * opacity // 100)
resized_watermark.putalpha(alpha)
except Exception as e:
messagebox.showerror("透明度处理错误", "出现错误,请检查水印图像。")
# 创建空白图像,用于粘贴水印图片,大小为原水印图片的4倍,背景为透明色
blank_image_dims = (round(4 * watermark_image.width),
round(4 * watermark_image.height))
my_watermark = Image.new('RGBA', blank_image_dims, (255, 255, 255, 0))
# 把水印图片粘贴在空白图像的中心位置,此方法可以避免原水印图片旋转过程导致尺寸变形
watermark_position = (int(my_watermark.width/2), int(my_watermark.height/2))
my_watermark.paste(resized_watermark, watermark_position)
# 旋转图像
try:
# 将文本图像修剪为仅围绕文本中心旋转的图像
text_box_size = my_watermark.getbbox()
my_watermark = my_watermark.crop(text_box_size)
new_watermark_image = my_watermark.rotate(current_tilt.get(),
center=(my_watermark.width / 2, my_watermark.height / 2),
expand=True)
except Exception as e:
return
# 添加水印
image.paste(new_watermark_image, paste_position, new_watermark_image)
# 保存结果图片,压缩质量为95%最高质量
image.save(output_path, quality=95)
except Exception as e:
messagebox.showerror("错误", "发生错误: " + str(e))
(4)文件夹内首张水印图片预览代码
# 图片水印预览函数,尝试打开指定文件夹内的第一个图像文件,添加水印后显示预览
def apply_watermark_preview():
open_image = open_image_entry.get()
watermark_path = watermark_entry.get()
position = position_var.get()
opacity = opacity_var.get().rstrip('%')
opacity = int(opacity)
watermark_scale_percent = scale_var.get().rstrip('%')
watermark_scale_percent = int(watermark_scale_percent)
if not open_image or not watermark_path:
return
# 定义图片扩展名列表
image_extensions = ['*.png', '*.jpg', '*.jpeg', '*.gif', '*.bmp']
for extension in image_extensions:
image_files = glob.glob(os.path.join(open_image, extension))
if image_files:
try: # 尝试打开第一个图像文件,添加水印后显示预览
with Image.open(image_files[0]) as img:
# 打开原始图片
image_width, image_height = img.size
# 打开水印图片并确保其为RGBA模式
watermark_image = Image.open(watermark_path)
if watermark_image.mode!= 'RGBA':
watermark_image = watermark_image.convert('RGBA')
watermark_width, watermark_height = watermark_image.size
# 将百分比转换为小数
scale_percent = watermark_scale_percent / 100
new_watermark_width = int(watermark_width * scale_percent)
new_watermark_height = int(watermark_height * scale_percent)
resized_watermark = watermark_image.resize((new_watermark_width, new_watermark_height))
# 根据选择的位置调整水印位置
if position == "左上":
paste_position = (0, 0)
elif position == "右上":
paste_position = (image_width - new_watermark_width, 0)
elif position == "居中":
paste_position = (int((image_width - new_watermark_width)/2), int((image_height - new_watermark_height)/2))
elif position == "左下":
paste_position = (0, image_height - new_watermark_height)
elif position == "右下":
paste_position = (image_width - new_watermark_width, image_height - new_watermark_height)
else:
paste_position = (0, 0)
# 处理透明度
try:
if opacity < 100:
alpha = resized_watermark.split()[3]
alpha = alpha.point(lambda p: p * opacity // 100)
resized_watermark.putalpha(alpha)
except Exception as e:
messagebox.showerror("透明度处理错误", "出现错误,请检查水印图像。")
# 创建空白图像,用于粘贴水印图片,大小为原水印图片的4倍,背景为透明色
blank_image_dims = (round(4 * watermark_image.width),
round(4 * watermark_image.height))
my_watermark = Image.new('RGBA', blank_image_dims, (255, 255, 255, 0))
# 把水印图片粘贴在空白图像的中心位置,此方法可以避免原水印图片旋转过程导致尺寸变形
watermark_position = (int(my_watermark.width/2), int(my_watermark.height/2))
my_watermark.paste(resized_watermark, watermark_position)
# 旋转图像
try:
# 将文本图像修剪为仅围绕文本中心旋转的图像
text_box_size = my_watermark.getbbox()
my_watermark = my_watermark.crop(text_box_size)
new_watermark_image = my_watermark.rotate(current_tilt.get(),
center=(my_watermark.width / 2, my_watermark.height / 2),
expand=True)
except Exception as e:
return
# 添加水印
img.paste(new_watermark_image, paste_position, new_watermark_image)
# 复制图片,创建带有水印的预览图像
preview_image = img.copy()
# 设置固定的预览框尺寸
target_width = 400
target_height = 400
# 获取原始图片宽高比
width_ratio = preview_image.width / target_width
height_ratio = preview_image.height / target_height
# 根据宽高比计算缩放后的尺寸,保持图片比例不变
if width_ratio > height_ratio:
new_width = target_width
new_height = int(preview_image.height / width_ratio)
else:
new_height = target_height
new_width = int(preview_image.width / height_ratio)
# 缩放图片
preview_image = preview_image.resize((new_width, new_height), Image.Resampling.LANCZOS)
# 计算在预览框中居中显示的偏移量
offset_x = (target_width - new_width) // 2
offset_y = (target_height - new_height) // 2
# 创建一个新的白色背景的图片(尺寸为预览框大小)
final_preview_image = Image.new('RGB', (target_width, target_height), (255, 255, 255))
final_preview_image.paste(preview_image, (offset_x, offset_y))
# 转换为Tkinter可以显示的格式
preview_photo = ImageTk.PhotoImage(final_preview_image)
preview_canvas.create_image(0, 0, anchor='nw', image=preview_photo)
preview_canvas.image = preview_photo
except Exception as e:
messagebox.showerror("预览错误", "无法显示水印预览: " + str(e))
(5)批量给图片添加图片水印的完整代码如下所示:
动手练一练:
import os
import tkinter as tk
from tkinter import filedialog, messagebox, ttk
from PIL import Image, ImageTk
import time
import glob
# 图片水印制作函数
def add_watermark(open_image, watermark_path, output_folder_path, position, opacity, watermark_scale_percent):
try:
# 获取原始图片文件名(包含扩展名)
open_image_name = os.path.basename(open_image)
file_name, file_extension = os.path.splitext(open_image_name)
# 水印完成后,创建输出文件名,在原始文件名后添加_时间戳后缀
# 获取当前时间的时间元组
current_time = time.localtime()
# 将时间元组格式化为字符串,例如,将时间“2025年08月23日09时22分30秒”格式化为“20250823092230”
time_str = time.strftime("%Y%m%d%H%M%S", current_time)
output_image_name = file_name + "_" + time_str + file_extension
# 构建完整的输出文件路径
output_path = os.path.join(output_folder_path, output_image_name)
# 打开原始图片
image = Image.open(open_image)
# 获取原始图片宽度和高度
image_width, image_height = image.size
# 打开水印图片并确保其为RGBA模式
watermark_image = Image.open(watermark_path)
if watermark_image.mode!= 'RGBA':
watermark_image = watermark_image.convert('RGBA')
watermark_width, watermark_height = watermark_image.size
# 将百分比转换为小数
scale_percent = watermark_scale_percent / 100
new_watermark_width = int(watermark_width * scale_percent)
new_watermark_height = int(watermark_height * scale_percent)
resized_watermark = watermark_image.resize((new_watermark_width, new_watermark_height))
# 根据选择的位置调整水印位置
if position == "左上":
paste_position = (0, 0)
elif position == "右上":
paste_position = (image_width - new_watermark_width, 0)
elif position == "居中":
paste_position = (int((image_width - new_watermark_width)/2), int((image_height - new_watermark_height)/2))
elif position == "左下":
paste_position = (0, image_height - new_watermark_height)
elif position == "右下":
paste_position = (image_width - new_watermark_width, image_height - new_watermark_height)
else:
paste_position = (0, 0)
# 处理透明度
try:
if opacity < 100:
alpha = resized_watermark.split()[3]
alpha = alpha.point(lambda p: p * opacity // 100)
resized_watermark.putalpha(alpha)
except Exception as e:
messagebox.showerror("透明度处理错误", "出现错误,请检查水印图像。")
# 创建空白图像,用于粘贴水印图片,大小为原水印图片的4倍,背景为透明色
blank_image_dims = (round(4 * watermark_image.width),
round(4 * watermark_image.height))
my_watermark = Image.new('RGBA', blank_image_dims, (255, 255, 255, 0))
# 把水印图片粘贴在空白图像的中心位置,此方法可以避免原水印图片旋转过程导致尺寸变形
watermark_position = (int(my_watermark.width/2), int(my_watermark.height/2))
my_watermark.paste(resized_watermark, watermark_position)
# 旋转图像
try:
# 将文本图像修剪为仅围绕文本中心旋转的图像
text_box_size = my_watermark.getbbox()
my_watermark = my_watermark.crop(text_box_size)
new_watermark_image = my_watermark.rotate(current_tilt.get(),
center=(my_watermark.width / 2, my_watermark.height / 2),
expand=True)
except Exception as e:
return
# 添加水印
image.paste(new_watermark_image, paste_position, new_watermark_image)
# 保存结果图片,压缩质量为95%最高质量
image.save(output_path, quality=95)
except Exception as e:
messagebox.showerror("错误", "发生错误: " + str(e))
# 打开文件选择对话框,用于选择需要批量添加水印的文件夹,文件夹内必须包含图片文件,否则无法选择文件夹
def select_batch_folder():
folder_path = filedialog.askdirectory(title="选择文件夹")
if folder_path:
open_image_entry.delete(0, tk.END)
open_image_entry.insert(0, folder_path)
open_image = open_image_entry.get()
# 定义图片扩展名列表
image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp']
# 遍历目录下的所有文件
files = os.listdir(open_image)
# 判断文件是否为图片文件
image_files = [file for file in files if os.path.splitext(file)[1].lower() in image_extensions]
# 如果不存在图片文件,则弹出警告,并取消选择该文件夹
if not image_files:
messagebox.showwarning("警告", "你选择的文件夹内没有图片文件!")
# 清除选择
open_image_entry.delete(0, tk.END)
# 打开文件选择对话框,用于选择“水印图片”
def browse_watermark_image():
file_selected = filedialog.askopenfilename()
if file_selected:
watermark_entry.delete(0, tk.END)
watermark_entry.insert(0, file_selected)
watermark_image = preprocess_watermark_image(file_selected)
if watermark_image:
apply_watermark_preview()
# 打开一个文件选择对话框,让用户选择水印图片保存后的目录
def browse_output_image():
folder_selected = filedialog.askdirectory()
if folder_selected:
output_entry.delete(0, tk.END)
output_entry.insert(0, folder_selected)
# 添加水印进程
def add_watermark_process():
open_image = open_image_entry.get()
watermark_path = watermark_entry.get()
output_folder_path = output_entry.get()
position = position_var.get()
opacity = opacity_var.get().rstrip('%')
opacity = int(opacity)
watermark_scale_percent = scale_var.get().rstrip('%')
watermark_scale_percent = int(watermark_scale_percent)
# 判断是否为空
if not open_image:
messagebox.showwarning("警告", "“待加水印文件夹”不能为空!")
return
elif not watermark_path:
messagebox.showwarning("警告", "“水印图片”不能为空!")
return
elif not output_folder_path:
messagebox.showwarning("警告", "“输出文件夹”不能为空!")
return
# 遍历文件夹中的所有图片文件,并分别添加图片水印
for app, dirs, files in os.walk(open_image):
for file in files:
file_extension = os.path.splitext(file)[1].lower()
if file_extension in [".png", ".jpg", ".jpeg", ".bmp", ".gif"]:
image_path = os.path.join(app, file)
add_watermark(image_path, watermark_path, output_folder_path, position, opacity, watermark_scale_percent)
messagebox.showinfo("完成", "水印已成功添加!")
# 图片水印预览函数,尝试打开指定文件夹内的第一个图像文件,添加水印后显示预览
def apply_watermark_preview():
open_image = open_image_entry.get()
watermark_path = watermark_entry.get()
position = position_var.get()
opacity = opacity_var.get().rstrip('%')
opacity = int(opacity)
watermark_scale_percent = scale_var.get().rstrip('%')
watermark_scale_percent = int(watermark_scale_percent)
if not open_image or not watermark_path:
return
# 定义图片扩展名列表
image_extensions = ['*.png', '*.jpg', '*.jpeg', '*.gif', '*.bmp']
for extension in image_extensions:
image_files = glob.glob(os.path.join(open_image, extension))
if image_files:
try: # 尝试打开第一个图像文件,添加水印后显示预览
with Image.open(image_files[0]) as img:
# 打开原始图片
image_width, image_height = img.size
# 打开水印图片并确保其为RGBA模式
watermark_image = Image.open(watermark_path)
if watermark_image.mode!= 'RGBA':
watermark_image = watermark_image.convert('RGBA')
watermark_width, watermark_height = watermark_image.size
# 将百分比转换为小数
scale_percent = watermark_scale_percent / 100
new_watermark_width = int(watermark_width * scale_percent)
new_watermark_height = int(watermark_height * scale_percent)
resized_watermark = watermark_image.resize((new_watermark_width, new_watermark_height))
# 根据选择的位置调整水印位置
if position == "左上":
paste_position = (0, 0)
elif position == "右上":
paste_position = (image_width - new_watermark_width, 0)
elif position == "居中":
paste_position = (int((image_width - new_watermark_width)/2), int((image_height - new_watermark_height)/2))
elif position == "左下":
paste_position = (0, image_height - new_watermark_height)
elif position == "右下":
paste_position = (image_width - new_watermark_width, image_height - new_watermark_height)
else:
paste_position = (0, 0)
# 处理透明度
try:
if opacity < 100:
alpha = resized_watermark.split()[3]
alpha = alpha.point(lambda p: p * opacity // 100)
resized_watermark.putalpha(alpha)
except Exception as e:
messagebox.showerror("透明度处理错误", "出现错误,请检查水印图像。")
# 创建空白图像,用于粘贴水印图片,大小为原水印图片的4倍,背景为透明色
blank_image_dims = (round(4 * watermark_image.width),
round(4 * watermark_image.height))
my_watermark = Image.new('RGBA', blank_image_dims, (255, 255, 255, 0))
# 把水印图片粘贴在空白图像的中心位置,此方法可以避免原水印图片旋转过程导致尺寸变形
watermark_position = (int(my_watermark.width/2), int(my_watermark.height/2))
my_watermark.paste(resized_watermark, watermark_position)
# 旋转图像
try:
# 将文本图像修剪为仅围绕文本中心旋转的图像
text_box_size = my_watermark.getbbox()
my_watermark = my_watermark.crop(text_box_size)
new_watermark_image = my_watermark.rotate(current_tilt.get(),
center=(my_watermark.width / 2, my_watermark.height / 2),
expand=True)
except Exception as e:
return
# 添加水印
img.paste(new_watermark_image, paste_position, new_watermark_image)
# 复制图片,创建带有水印的预览图像
preview_image = img.copy()
# 设置固定的预览框尺寸
target_width = 400
target_height = 400
# 获取原始图片宽高比
width_ratio = preview_image.width / target_width
height_ratio = preview_image.height / target_height
# 根据宽高比计算缩放后的尺寸,保持图片比例不变
if width_ratio > height_ratio:
new_width = target_width
new_height = int(preview_image.height / width_ratio)
else:
new_height = target_height
new_width = int(preview_image.width / height_ratio)
# 缩放图片
preview_image = preview_image.resize((new_width, new_height), Image.Resampling.LANCZOS)
# 计算在预览框中居中显示的偏移量
offset_x = (target_width - new_width) // 2
offset_y = (target_height - new_height) // 2
# 创建一个新的白色背景的图片(尺寸为预览框大小)
final_preview_image = Image.new('RGB', (target_width, target_height), (255, 255, 255))
final_preview_image.paste(preview_image, (offset_x, offset_y))
# 转换为Tkinter可以显示的格式
preview_photo = ImageTk.PhotoImage(final_preview_image)
preview_canvas.create_image(0, 0, anchor='nw', image=preview_photo)
preview_canvas.image = preview_photo
except Exception as e:
messagebox.showerror("预览错误", "无法显示水印预览: " + str(e))
# 选择水印位置时触发的函数
def on_position_change(event):
apply_watermark_preview()
# 选择水印透明度时触发的函数
def on_opacity_change(event):
apply_watermark_preview()
# 选择水印缩放比例时触发的函数
def on_scale_change(event):
apply_watermark_preview()
# Spinbox控件点击箭头输入数字时触发的函数
def on_tilt_change(event):
apply_watermark_preview()
# Spinbox控件直接输入数字时触发的函数
def on_Release_change(event):
# Spinbox控件输入不合法时修改为0。
if not tilt_spinbox.get().isdigit():
tilt_spinbox.delete(0, tk.END)
tilt_spinbox.insert(0, '0') # 将值重置为0
apply_watermark_preview()
# 判断水印图片是否为RGBA模式,如果不是,则将图片转换为RGBA模式
def preprocess_watermark_image(watermark_path):
try:
watermark_image = Image.open(watermark_path)
if watermark_image.mode!= 'RGBA':
watermark_image = watermark_image.convert('RGBA')
return watermark_image
except Exception as e:
messagebox.showerror("水印图像预处理错误", "出现错误,请检查水印图像。")
return None
# 创建主窗口
app = tk.Tk()
app.title("批量给图片添加图片水印工具")
app.geometry("950x500")
app.resizable(False, False)
# 设置主窗口居中
app.update_idletasks()
window_width = app.winfo_width()
window_height = app.winfo_height()
screen_width = app.winfo_screenwidth()
screen_height = app.winfo_screenheight()
x = (screen_width - window_width) // 2
y = (screen_height - window_height) // 2
app.geometry(f"{window_width}x{window_height}+{x}+{y}")
# 添加水印预览区域
preview_frame = tk.Frame(app)
preview_frame.pack(side="left", pady=10, padx=20)
preview_label = tk.Label(preview_frame, text="文件夹内首张水印图片预览:")
preview_label.grid(column=0, row=0, pady=5)
preview_canvas = tk.Canvas(preview_frame, width=400, height=400, bg="white")
preview_canvas.grid(column=0, row=1, pady=5)
# 待加水印文件夹选择
edit_image_frame = tk.Frame(app)
edit_image_frame.pack(side="right", pady=10, padx=20)
open_image_label = tk.Label(edit_image_frame, text="选择待加水印文件夹:", width=18, anchor="w")
open_image_label.grid(column=0, row=0, pady=5, padx=2)
open_image_entry = tk.Entry(edit_image_frame, width=40)
open_image_entry.grid(column=1, row=0, pady=5, padx=2)
open_image_browse_button = tk.Button(edit_image_frame, text="浏览", command=select_batch_folder)
open_image_browse_button.grid(column=2, row=0, pady=5, padx=2)
# 水印图片选择
watermark_label = tk.Label(edit_image_frame, text="选择水印图片:", width=18, anchor="w")
watermark_label.grid(column=0, row=1, pady=5, padx=2)
watermark_entry = tk.Entry(edit_image_frame, width=40)
watermark_entry.grid(column=1, row=1, pady=5, padx=2)
watermark_browse_button = tk.Button(edit_image_frame, text="浏览", command=browse_watermark_image)
watermark_browse_button.grid(column=2, row=1, pady=5, padx=2)
# 输出图片选择
output_label = tk.Label(edit_image_frame, text="选择输出文件夹:", width=18, anchor="w")
output_label.grid(column=0, row=2, pady=5, padx=2)
output_entry = tk.Entry(edit_image_frame, width=40)
output_entry.grid(column=1, row=2, pady=5, padx=2)
output_browse_button = tk.Button(edit_image_frame, text="浏览", command=browse_output_image)
output_browse_button.grid(column=2, row=2, pady=5, padx=2)
# 水印位置选择
position_label = tk.Label(edit_image_frame, pady=5, padx=2, text="水印位置:", width=18, anchor="w")
position_label.grid(column=0, row=3)
position_var = tk.StringVar()
position_option = ttk.Combobox(
edit_image_frame,
textvariable=position_var,
values=["左上", "右上", "居中", "左下", "右下"],
width=10,
state="readonly",
)
position_option.current(2)
position_option.grid(column=1, row=3, pady=5, padx=2, sticky="w")
position_option.bind("<<ComboboxSelected>>", on_position_change)
# 水印透明度选择
opacity_label = tk.Label(edit_image_frame, text="水印透明度:", width=18, anchor="w")
opacity_label.grid(column=0, row=4, pady=5, padx=2)
opacity_var = tk.StringVar()
opacity_option = ttk.Combobox(
edit_image_frame,
textvariable=opacity_var,
values=["0%", "10%", "20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%", "100%"],
width=10,
state="readonly",
)
# 默认选择100%
opacity_option.current(10)
opacity_option.grid(column=1, row=4, pady=5, padx=2, sticky="w")
opacity_option.bind("<<ComboboxSelected>>", on_opacity_change)
# 水印缩放比例选择
scale_label = tk.Label(edit_image_frame, text="水印缩放比例:", width=18, anchor="w")
scale_label.grid(column=0, row=5, pady=5, padx=2)
scale_var = tk.StringVar()
scale_option = ttk.Combobox(
edit_image_frame,
textvariable=scale_var,
values=["3%", "5%", "10%", "20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%", "100%", "150%", "200%", "250%", "300%"],
width=10,
state="readonly",
)
# 默认选择100%
scale_option.current(11)
scale_option.grid(column=1, row=5, pady=5, padx=2, sticky="w")
scale_option.bind("<<ComboboxSelected>>", on_scale_change)
# 水印角度
current_tilt = tk.IntVar(value=0)
tilt_label = tk.Label(edit_image_frame, text="水印角度: ", width=18, anchor="w")
tilt_label.grid(column=0, row=6, pady=5, padx=2)
tilt_spinbox = tk.Spinbox(edit_image_frame,
from_=0,
to=359,
wrap=True,
textvariable=current_tilt,
)
tilt_spinbox.grid(column=1, row=6, pady=5, padx=2, sticky="w")
tilt_spinbox.bind("<ButtonRelease-1>", on_tilt_change)
tilt_spinbox.bind("<KeyRelease>", on_Release_change)
# 开始添加水印按钮
start_button = tk.Button(edit_image_frame, text="批量添加水印", command=add_watermark_process)
start_button.grid(column=0, row=7, pady=10, columnspan=2)
# 当前模块直接被执行
if __name__ == "__main__":
# 运行主循环
app.mainloop()