diff --git a/猿人学Web端爬虫攻防刷题平台/猿人学第8题-验证码图文点选/gan_rao_xian.py b/猿人学Web端爬虫攻防刷题平台/猿人学第8题-验证码图文点选/gan_rao_xian.py index ab18829..dd4e3ea 100644 --- a/猿人学Web端爬虫攻防刷题平台/猿人学第8题-验证码图文点选/gan_rao_xian.py +++ b/猿人学Web端爬虫攻防刷题平台/猿人学第8题-验证码图文点选/gan_rao_xian.py @@ -15,19 +15,22 @@ def turn_white(img, r1, g1, b1): def processing_image(img_file, standard=205): """ 1.将图片进行降噪处理, 通过二值化去掉后面的背景色并加深文字对比度 """ img = Image.open(img_file) - # for i in range(img.size[0]): - # for j in range(img.size[1]): - # r, g, b = img.getpixel((i, j)) - # print([r, g, b]) - - - # np.unique()该函数是去除数组中的重复数字,并进行排序之后输出,这里返回的是所有像素rgb值,以及对应的数量 + # colors所有像素rgb值,counts对应的数量 colors, counts = np.unique(np.array(img).reshape(-1, 3), axis=0, return_counts=True) - print(colors, counts) - # ct = np.sort(counts) - # top2_counts = ct[-2:].tolist() - # print(np.array(im).reshape(-1, 3)) - + # 排序 + ct = np.sort(counts) + # 找到出现最多的2种颜色的个数 + top2_counts = ct[-2:].tolist() + # 找到出现最多的2种颜色的下标 + subscript_list = [] + for k, v in list(enumerate(counts, start=0)): + if v in top2_counts: + subscript_list.append(k) + # 找到出现最多的2种颜色的rgb值 + for subscript in subscript_list: + color = colors[subscript] + # 去除颜色 + turn_white(img, color[0], color[1], color[2]) return img @@ -38,4 +41,4 @@ def run(): if __name__ == '__main__': - run() \ No newline at end of file + run() diff --git a/猿人学Web端爬虫攻防刷题平台/猿人学第8题-验证码图文点选/img/1-test.png b/猿人学Web端爬虫攻防刷题平台/猿人学第8题-验证码图文点选/img/1-test.png index 38d3afb..68ae117 100644 Binary files a/猿人学Web端爬虫攻防刷题平台/猿人学第8题-验证码图文点选/img/1-test.png and b/猿人学Web端爬虫攻防刷题平台/猿人学第8题-验证码图文点选/img/1-test.png differ