使用Python PIL库实现简单验证码的去噪处理(2)

# encoding=utf8 from PIL import Image def get_bin_table(threshold=115): ''' 获取灰度转二值的映射table 0表示黑色,1表示白色 ''' table = [] for i in range(256): if i < threshold: table.append(0) else: table.append(1) return table def main(): image = Image.open('RandomPicture.png') imgry = image.convert('L') table = get_bin_table() binary = imgry.point(table, '1') binary.save('binary.png') if __name__ == '__main__': main()

使用Python PIL库实现简单验证码的去噪处理

运行结果:

使用Python PIL库实现简单验证码的去噪处理

通过结果不难看出,我们已经将最开始的彩色图由灰度图转变成了仅由黑白二色组成的图片,实现了二值化。这里需要说明的是,threshold参数值针对当前的验证码图片合适,该值需要根据验证码类型不同进行调试来确定。

然后我们再看看(0, 0)坐标代表的像素的颜色值是什么:

# encoding=utf8 from PIL import Image def get_bin_table(threshold=115): ''' 获取灰度转二值的映射table 0表示黑色,1表示白色 ''' table = [] for i in range(256): if i < threshold: table.append(0) else: table.append(1) return table def main(): image = Image.open('RandomPicture.png') print 'image mode: ', image.mode print image.getpixel((0, 0)) co = image.getcolors() print co print '-' * 40 imgry = image.convert('L') print 'imgry mode: ', imgry.mode print imgry.getpixel((0, 0)) co = imgry.getcolors() print co print '-' * 40 table = get_bin_table() binary = imgry.point(table, '1') print 'binary mode: ', binary.mode print binary.getpixel((0, 0)) co = binary.getcolors() print co if __name__ == '__main__': main()

运行结果:

image mode:  RGB
(234, 235, 236)
None
----------------------------------------
imgry mode:  L
234
[(1, 16), (1, 17), (3, 18), (3, 20), (3, 21), (2, 22), (3, 24), (2, 25), (1, 26), (3, 28), (1, 29), (6, 31), (15, 32), (136, 33), (193, 34), (115, 35), (52, 36), (25, 37), (24, 38), (16, 39), (7, 40), (4, 41), (3, 42), (4, 43), (2, 44), (4, 45), (4, 46), (2, 48), (4, 49), (2, 50), (4, 51), (4, 52), (2, 53), (11, 54), (18, 55), (21, 56), (71, 57), (61, 58), (24, 59), (15, 60), (17, 61), (11, 62), (8, 63), (11, 64), (8, 65), (10, 66), (6, 67), (4, 68), (5, 69), (9, 70), (7, 71), (11, 72), (11, 73), (6, 74), (16, 75), (22, 76), (15, 77), (19, 78), (12, 79), (16, 80), (11, 81), (17, 82), (24, 83), (29, 84), (18, 85), (35, 86), (18, 87), (22, 88), (23, 89), (25, 90), (19, 91), (21, 92), (19, 93), (29, 94), (25, 95), (20, 96), (17, 97), (15, 98), (16, 99), (24, 100), (21, 101), (14, 102), (23, 103), (20, 104), (15, 105), (15, 106), (12, 107), (16, 108), (19, 109), (18, 110), (10, 111), (15, 112), (16, 113), (31, 114), (16, 115), (21, 116), (15, 117), (15, 118), (19, 119), (15, 120), (14, 121), (23, 122), (17, 123), (17, 124), (17, 125), (10, 126), (19, 127), (18, 128), (10, 129), (11, 130), (9, 131), (13, 132), (21, 133), (12, 134), (18, 135), (15, 136), (9, 137), (20, 138), (20, 139), (15, 140), (21, 141), (30, 142), (30, 143), (35, 144), (35, 145), (122, 146), (43, 147), (36, 148), (32, 149), (21, 150), (37, 151), (30, 152), (45, 153), (33, 154), (31, 155), (37, 156), (37, 157), (37, 158), (33, 159), (51, 160), (64, 161), (47, 162), (32, 163), (39, 164), (48, 165), (51, 166), (55, 167), (126, 168), (64, 169), (217, 170), (263, 171), (85, 172), (44, 173), (39, 174), (44, 175), (38, 176), (60, 177), (43, 178), (54, 179), (47, 180), (64, 181), (55, 182), (56, 183), (44, 184), (66, 185), (64, 186), (56, 187), (66, 188), (68, 189), (86, 190), (108, 191), (93, 192), (112, 193), (83, 194), (67, 195), (68, 196), (75, 197), (65, 198), (69, 199), (70, 200), (88, 201), (111, 202), (71, 203), (74, 204), (83, 205), (73, 206), (94, 207), (119, 208), (92, 209), (114, 210), (89, 211), (87, 212), (96, 213), (106, 214), (85, 215), (82, 216), (103, 217), (106, 218), (99, 219), (140, 220), (134, 221), (126, 222), (114, 223), (137, 224), (140, 225), (137, 226), (148, 227), (137, 228), (126, 229), (118, 230), (135, 231), (137, 232), (120, 233), (187, 234), (168, 235), (226, 236), (358, 237), (354, 238), (288, 239), (194, 240), (201, 241), (219, 242), (245, 243), (250, 244), (303, 245), (303, 246), (389, 247), (412, 248), (569, 249), (792, 250), (1410, 251), (3198, 252), (5163, 253), (6040, 254), (2028, 255)]
----------------------------------------
binary mode:  1
1
[(1768, 0), (30998, 1)]
[Finished in 0.1s]

使用Python PIL库实现简单验证码的去噪处理

代码说明:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/83218f1cb4dec2dadc339a7fd3aa263c.html