猿人学第8题-验证码图文点选

This commit is contained in:
aiyingfeng 2023-06-25 20:50:50 +08:00
parent 702367c7d4
commit fe3910656e

View File

@ -12,8 +12,8 @@ def turn_white(img, r1, g1, b1):
img.putpixel((i, j), (255, 255, 255)) img.putpixel((i, j), (255, 255, 255))
def processing_image(img_file, standard=205): def noise_image(img_file):
""" 1.将图片进行降噪处理, 通过二值化去掉后面的背景色并加深文字对比度 """ """选出rgp颜色最多2个进行降噪处理"""
img = Image.open(img_file) img = Image.open(img_file)
# colors所有像素rgb值counts对应的数量 # colors所有像素rgb值counts对应的数量
colors, counts = np.unique(np.array(img).reshape(-1, 3), axis=0, return_counts=True) 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] color = colors[subscript]
# 去除颜色 # 去除颜色
turn_white(img, color[0], color[1], color[2]) turn_white(img, color[0], color[1], color[2])
return img 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(): def run():
image_b = processing_image('./img/1.png') image_a = noise_image('./img/1.png')
image_b.save('./img/1-test.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__': if __name__ == '__main__':