python中图片上的水印去除(包含批量处理) |
您所在的位置:网站首页 › 图片批量去除水印 › python中图片上的水印去除(包含批量处理) |
你们有没有遇到过文本文件中烦人的水印无法去除而烦恼。那么以下内容可以看看。 今天我就遇到了一个文本图片中的水印无法去除的情况,在某宝买短期会员后使用wps试了一下还是无法设别图片,后面试了很多软件和网页,由于水印较大且与字母叠在一起,还是不行(当然ps中是可以的,且可以批量处理。作者懒的安装ps且平时用不上,又不太会kirta中去水印)。最后想起python中试试。 思路:文本颜色(RGB)和水印颜色(RGB)不一样。 代码思路:我在代码中定义了一个名为removeFimage的函数,该函数接受三个参数:img_path(输入图像的路径),rgb_sum(每个像素RGB值之和的阈值)以及dest_path(保存处理后图像的输出路径)。 函数执行以下操作: 使用PIL库中的Image模块打开给定img_path处的图像。获取图像的宽度和高度。通过itertools.product(range(width), range(height))遍历图像中的每一个像素位置。对于每个像素,获取其RGB值,并检查它们的和是否大于或等于rgb_sum。如果满足条件,则将该像素替换为白色(255, 255, 255)。当主程序运行时,调用这个函数并传入实际的图片路径、RGB总和阈值以及目标保存路径。具体来说,它会把RGB总和超过250的像素颜色替换成白色,并将处理后的图像保存在'D:/桌面/1a.png'路径下。 提醒:作者在这里已查看水印RGB=(87,87,87) 整体代码: from PIL import Image from tqdm import tqdm def remove_pixels(img_path, rgb_sum_threshold, dest_path): def is_high_brightness(rgb): return sum(rgb) >= rgb_sum_threshold with Image.open(img_path) as img: width, height = img.size for x in tqdm(range(width), desc="Processing Columns"): for y in range(height): if is_high_brightness(img.getpixel((x, y))[:3]): img.putpixel((x, y), (255, 255, 255)) img.save(dest_path) if __name__ == '__main__': remove_pixels('D:/桌面/1.png', 250, 'D:/桌面/1a.png')当图片较多需要批量处理时: from PIL import Image from tqdm import tqdm import os def remove_pixels(img_path, rgb_sum_threshold, dest_path): def is_high_brightness(rgb): return sum(rgb) >= rgb_sum_threshold with Image.open(img_path) as img: width, height = img.size for x in tqdm(range(width), desc="Processing Columns"): for y in range(height): if is_high_brightness(img.getpixel((x, y))[:3]): img.putpixel((x, y), (255, 255, 255)) img.save(dest_path) if __name__ == '__main__': root_dir = 'E:/pth' output_dir = 'E:/output' # 设置输出目录 rgb_sum_threshold = 250 for i in range(1, 51): input_file = f'{root_dir}/作品{str(i).zfill(2)}.png' # 格式化文件名 output_file = f'{output_dir}/作品{str(i).zfill(2)}_processed.png' if os.path.exists(input_file): remove_pixels(input_file, rgb_sum_threshold, output_file) else: print(f"File {input_file} does not exist.")当然当图片比较复杂或内容较多时效果比不上ps等专业软件(花费时间比较长,所有代码中加入了进度条)。同样的道理可以处理pdf,word等文件,若想提高速度可以尝试减少循环或者用: 并行处理:将图片分割为多个部分,利用多线程或多进程同时处理这些部分。Python中可以使用concurrent.futures模块实现。Numpy加速:如果可能的话,尝试将PIL图像转换为numpy数组进行操作,然后再转换回PIL图像。Numpy对大型数据的操作通常比纯Python更快。留言:希望对你们有所帮助,若有疑问可以给B站up:Hikmatkar留言。 祝大家龙年吉祥,顺心如意。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |