Python 白底黑字图片去除红色水印 import os from PIL import Image def remove_color(image_path, new_image_path): """ 初始化 :param image_path: 图片路径 :param new_image_path: 新图片路径 """ # 打开图片并转换为RGBA格式 img = Image.open(image_path).convert("RGBA") datas = img.getdata() # 遍历每个像素 new_data = [] for item in datas: # 用取色器识别水印颜色将RGB值相加 得出一个RGB和的范围 if sum(item[:3]) in range(300, 765): # 转化为白色 new_data.append((255, 255, 255, 1)) else: new_data.append(item) # 将修改后的数据重新写入图片 img.putdata(new_data) img.save(new_image_path, "png") if __name__ == '__main__': file_name_path = 'photo/image.png' new_file_name_path = 'photo/new_image.png' remove_color(file_name_path, new_file_name_path)