当前位置:   article > 正文

python中图片上的水印去除(包含批量处理)_python 图片批量去水印

python 图片批量去水印

你们有没有遇到过文本文件中烦人的水印无法去除而烦恼。那么以下内容可以看看。

今天我就遇到了一个文本图片中的水印无法去除的情况,在某宝买短期会员后使用wps试了一下还是无法设别图片,后面试了很多软件和网页,由于水印较大且与字母叠在一起,还是不行(当然ps中是可以的,且可以批量处理。作者懒的安装ps且平时用不上,又不太会kirta中去水印)。最后想起python中试试。

思路:文本颜色(RGB)和水印颜色(RGB)不一样。

代码思路:我在代码中定义了一个名为removeFimage的函数,该函数接受三个参数:img_path(输入图像的路径),rgb_sum(每个像素RGB值之和的阈值)以及dest_path(保存处理后图像的输出路径)。

函数执行以下操作:

  1. 使用PIL库中的Image模块打开给定img_path处的图像。
  2. 获取图像的宽度和高度。
  3. 通过itertools.product(range(width), range(height))遍历图像中的每一个像素位置。
  4. 对于每个像素,获取其RGB值,并检查它们的和是否大于或等于rgb_sum
  5. 如果满足条件,则将该像素替换为白色(255, 255, 255)

当主程序运行时,调用这个函数并传入实际的图片路径、RGB总和阈值以及目标保存路径。具体来说,它会把RGB总和超过250的像素颜色替换成白色,并将处理后的图像保存在'D:/桌面/1a.png'路径下。

提醒:作者在这里已查看水印RGB=(87,87,87)

整体代码:

  1. from PIL import Image
  2. from tqdm import tqdm
  3. def remove_pixels(img_path, rgb_sum_threshold, dest_path):
  4. def is_high_brightness(rgb):
  5. return sum(rgb) >= rgb_sum_threshold
  6. with Image.open(img_path) as img:
  7. width, height = img.size
  8. for x in tqdm(range(width), desc="Processing Columns"):
  9. for y in range(height):
  10. if is_high_brightness(img.getpixel((x, y))[:3]):
  11. img.putpixel((x, y), (255, 255, 255))
  12. img.save(dest_path)
  13. if __name__ == '__main__':
  14. remove_pixels('D:/桌面/1.png', 250, 'D:/桌面/1a.png')

当图片较多需要批量处理时:

  1. from PIL import Image
  2. from tqdm import tqdm
  3. import os
  4. def remove_pixels(img_path, rgb_sum_threshold, dest_path):
  5. def is_high_brightness(rgb):
  6. return sum(rgb) >= rgb_sum_threshold
  7. with Image.open(img_path) as img:
  8. width, height = img.size
  9. for x in tqdm(range(width), desc="Processing Columns"):
  10. for y in range(height):
  11. if is_high_brightness(img.getpixel((x, y))[:3]):
  12. img.putpixel((x, y), (255, 255, 255))
  13. img.save(dest_path)
  14. if __name__ == '__main__':
  15. root_dir = 'E:/pth'
  16. output_dir = 'E:/output' # 设置输出目录
  17. rgb_sum_threshold = 250
  18. for i in range(1, 51):
  19. input_file = f'{root_dir}/作品{str(i).zfill(2)}.png' # 格式化文件名
  20. output_file = f'{output_dir}/作品{str(i).zfill(2)}_processed.png'
  21. if os.path.exists(input_file):
  22. remove_pixels(input_file, rgb_sum_threshold, output_file)
  23. else:
  24. print(f"File {input_file} does not exist.")

当然当图片比较复杂或内容较多时效果比不上ps等专业软件(花费时间比较长,所有代码中加入了进度条)。同样的道理可以处理pdf,word等文件,若想提高速度可以尝试减少循环或者用:

  1. 并行处理:将图片分割为多个部分,利用多线程或多进程同时处理这些部分。Python中可以使用concurrent.futures模块实现。
  2. Numpy加速:如果可能的话,尝试将PIL图像转换为numpy数组进行操作,然后再转换回PIL图像。Numpy对大型数据的操作通常比纯Python更快。

留言:希望对你们有所帮助,若有疑问可以给B站up:Hikmatkar留言。

祝大家龙年吉祥,顺心如意。

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号