From fe3910656e2c37a511d857b1d07e3ab068989f03 Mon Sep 17 00:00:00 2001 From: aiyingfeng Date: Sun, 25 Jun 2023 20:50:50 +0800 Subject: [PATCH] =?UTF-8?q?=E7=8C=BF=E4=BA=BA=E5=AD=A6=E7=AC=AC8=E9=A2=98-?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E7=A0=81=E5=9B=BE=E6=96=87=E7=82=B9=E9=80=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../猿人学第8题-验证码图文点选/gan_rao_xian.py | 60 +++++++++++++++++-- 1 file changed, 55 insertions(+), 5 deletions(-) diff --git a/猿人学Web端爬虫攻防刷题平台/猿人学第8题-验证码图文点选/gan_rao_xian.py b/猿人学Web端爬虫攻防刷题平台/猿人学第8题-验证码图文点选/gan_rao_xian.py index dd4e3ea..4467293 100644 --- a/猿人学Web端爬虫攻防刷题平台/猿人学第8题-验证码图文点选/gan_rao_xian.py +++ b/猿人学Web端爬虫攻防刷题平台/猿人学第8题-验证码图文点选/gan_rao_xian.py @@ -12,8 +12,8 @@ def turn_white(img, r1, g1, b1): img.putpixel((i, j), (255, 255, 255)) -def processing_image(img_file, standard=205): - """ 1.将图片进行降噪处理, 通过二值化去掉后面的背景色并加深文字对比度 """ +def noise_image(img_file): + """选出rgp颜色最多2个,进行降噪处理""" img = Image.open(img_file) # colors所有像素rgb值,counts对应的数量 colors, counts = np.unique(np.array(img).reshape(-1, 3), axis=0, return_counts=True) @@ -31,13 +31,63 @@ def processing_image(img_file, standard=205): color = colors[subscript] # 去除颜色 turn_white(img, color[0], color[1], color[2]) - return img +def interference(img_file): + """移除干扰线条""" + img = Image.open(img_file) + # 读取图片高,宽 + height, width = img.height, img.width + # 首先创建一个空列表,用来存放出现在间隔当中的像素点 + line_list = [] + # 两个for循环,遍历9000次 + for x in range(width): + for y in range(height): + r, g, b = img.getpixel((x, y)) + if 0 < y < 10 or 96 < y < 105 or 199 < y < 209 or 292 < y < 299: + line_list.append([r, g, b]) + if 0 < x < 20 or 109 < x < 120 or 209 < x < 220: + line_list.append([r, g, b]) + + arr = np.array(line_list) + line_list = np.array(list(set([tuple(t) for t in arr]))) + # 处理像素 RGB 值 + for line in line_list: + r = line[0] + g = line[1] + b = line[2] + if not (r == 255 and g == 255 and b == 255): + print(r, g, b) + turn_white(img, r, g, b) + return img + + +def binary(img_file, standard=205): + """灰度转换""" + img = Image.open(img_file) + img = img.convert('L') + pixels = img.load() + for x in range(img.width): + for y in range(img.height): + if pixels[x, y] > standard: + pixels[x, y] = 255 + else: + pixels[x, y] = 0 + return img + + +def enhance(img_file): + """增强字体显示效果""" + + def run(): - image_b = processing_image('./img/1.png') - image_b.save('./img/1-test.png') + image_a = noise_image('./img/1.png') + image_a.save('./img/1-test.png') + image_b = interference('./img/1-test.png') + image_b.save('./img/2-test.png') + image_c = binary('./img/2-test.png') + image_c.save('./img/3-test.png') if __name__ == '__main__':