阿水的世界。

10月 13

python脚本纯色底色图片固定位置水印覆盖去除

最近从网上批量下来的图片有点多,都是白色底色,左上角固定位置有水印。我想把水印批量删了,photoshop有点慢,就用AI写了个python的脚本,批量处理图片。试了试还挺好用,处理300个图片大概几秒钟,挺快的。

    import os
from PIL import Image

def process_image(image_path, save_path, overwrite=False):
    try:
        # 打开图片
        img = Image.open(image_path)

        # 确保图片至少有150x60像素
        if img.width >= 150 and img.height >= 60:
            # 创建一个白色区域,大小150x60像素
            white_patch = Image.new('RGB', (150, 60), (255, 255, 255))
            
            # 将白色区域粘贴到图片的左上角
            img.paste(white_patch, (0, 0))
            
            # 保存修改后的图片
            if overwrite:
                img.save(save_path)
                print(f"Overwritten: {save_path}")
            else:
                base, ext = os.path.splitext(save_path)
                new_save_path = f"{base}_modified{ext}"
                img.save(new_save_path)
                print(f"Processed and saved: {new_save_path}")
        else:
            print(f"Image {image_path} is too small to process.")
    except Exception as e:
        print(f"Error processing {image_path}: {e}")

def batch_process_images(folder_path, overwrite=False):
    # 遍历文件夹中的所有文件
    for filename in os.listdir(folder_path):
        if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif')):
            image_path = os.path.join(folder_path, filename)
            save_path = os.path.join(folder_path, filename)
            process_image(image_path, save_path, overwrite)

# 设置你的图片文件夹路径
folder_path = 'c:\\static\\upload\\image\\20241010\\' # 替换为你的图片文件夹路径,\需要替换为\\
overwrite = True  # 设置为True以覆盖原图,设置为False以保存为新文件
batch_process_images(folder_path, overwrite)

 

 

 

标签:python, 去水印

还不快抢沙发

添加新评论

captcha