使用方式:运行代码,选择需要处理的图片所在文件夹即可,
[color=]ps:处理后的图片会直接覆盖原图
[Python] 纯文本查看 复制代码import os
from PIL import Image
from tkinter import filedialog
def remove_watermark(directory, watermark_height_ratio):
for filename in os.listdir(directory):
if filename.endswith('.jpg') or filename.endswith('.png'):
image_path = os.path.join(directory, filename)
image = Image.open(image_path)
width, height = image.size
# 根据水印占据的比例,计算水印的高度
watermark_height = int(height * watermark_height_ratio)
# 计算剪切区域的坐标
new_image = image.crop((0, 0, width, height - watermark_height))
new_image.save(image_path) # 直接覆盖原来的图片
directory = filedialog.askdirectory() # 打开文件对话框选择目录
remove_watermark(directory, 0.1)