赞
踩
你们有没有遇到过文本文件中烦人的水印无法去除而烦恼。那么以下内容可以看看。
今天我就遇到了一个文本图片中的水印无法去除的情况,在某宝买短期会员后使用wps试了一下还是无法设别图片,后面试了很多软件和网页,由于水印较大且与字母叠在一起,还是不行(当然ps中是可以的,且可以批量处理。作者懒的安装ps且平时用不上,又不太会kirta中去水印)。最后想起python中试试。
思路:文本颜色(RGB)和水印颜色(RGB)不一样。
代码思路:我在代码中定义了一个名为removeFimage
的函数,该函数接受三个参数:img_path
(输入图像的路径),rgb_sum
(每个像素RGB值之和的阈值)以及dest_path
(保存处理后图像的输出路径)。
函数执行以下操作:
img_path
处的图像。itertools.product(range(width), range(height))
遍历图像中的每一个像素位置。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等文件,若想提高速度可以尝试减少循环或者用:
concurrent.futures
模块实现。留言:希望对你们有所帮助,若有疑问可以给B站up:Hikmatkar留言。
祝大家龙年吉祥,顺心如意。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。