python 去掉验证码干扰线
干扰线是单一的颜色
import io from PIL import Image def del_noise(img): # 打开验证码图片 image = Image.open(io.BytesIO(img)) # image = Image.open('o4.png') # 获取图像的宽度和高度 width, height = image.size count_map = {} # 遍历每个像素并检查是否为横线干扰线 for y in range(height): for x in range(width): r, g, b, a = image.getpixel((x, y)) if (r, g, b) != (0, 0, 0) and (r, g, b) != (255, 255, 255): # print(r, g, b, a) count_map[(r, g, b)] = count_map.get((r, g, b), 0) + 1 # 找出count_map值最大的key max_key = max(count_map, key=count_map.get) # 遍历每个像素并检查是否为横线干扰线 for y in range(height): for x in range(width): # 获取像素值 r, g, b, a = image.getpixel((x, y)) # print(r, g, b, a) # 如果像素是黑色且上方和下方像素都是白色,则将其设置为白色 if (r, g, b) == max_key: try: if image.getpixel((x, y - 1)) == (0, 0, 0, 0) and image.getpixel((x, y + 1)) == (0, 0, 0, 0): image.putpixel((x, y), (0, 0, 0, 0)) except: pass # 保存处理后的图片 image.save('2222.png') # gray_image = image.convert('L') # gray_image.save('2222.png') if __name__ == '__main__': aa = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x000\x00\x00\x00\x14\x08\x06\x00\x00\x00\xcb?\xbe\xa7\x00\x00\x00\x91IDATx\xdac`\x18\xe4\xc0BL\xfa?><\xe0\x0e\xa0\x9a\x03-c\x9c\xfe#cj;@^N\xef?2&\xe4\x9e@)\x97\xff\xc8x@\xa3\x08\xdd\xd1\x84<\x81\xeeh\xbc\x9e\x18\xf2\x1e@O>0>5\x93\x10\xcd=\x80\xcd\x13\x84@h\xb6\xf3\x7fdL\xcbLL\x94\x07H\xf1\x04\xba\xa3\ty\x82\x92\x18 :\x89#'\x1fB\x9e\xa0\xa7\x07H\xca\x03\xb8\xf8\xa3\x1e\xa0w&\xa6v\x1e\xa0iEF\x0e \xc7\x03\x83\n\x8cz`0ybH:~\x14\x8c\x82a\x04\x005\x1a}\xd4\x8a\xc6\xf7\x07\x00\x00\x00\x00IEND\xaeB`\x82" del_noise(aa)