python 使用PIL库进行验证码清晰处理
python 使用PIL库进行验证码清晰处理
from PIL import Image
import sys
import os
sys.setrecursionlimit(1000000)
pixel_list = []
all_pixel_list = []
#二值化
def Binarization(image):
threshold = 160
table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
image = image.point(table, '1')
return image
def partition(image):
width = image.size[0]
height = image.size[1]
count = 1
for w in range(width):
for h in range(height):
pixel = image.getpixel((w, h))
if pixel == 0:
zuobiao = str(w) + ',' + str(h)
if zuobiao not in all_pixel_list:
print('起始像素', zuobiao)
if len(pixel_list) > 20:
pixel_list.clear()
all_pixel_list.append(zuobiao)
pixel_list.append(zuobiao)
check_around(w, h)
print(pixel_list)
if len(pixel_list) > 20:
count += 1
else:
if count != 1:
pass
def check_around(width,height):
shang = ( width , height-1)
xia = ( width , height+1 )
zuo = ( width-1, height)
you = ( width+1 , height)
if image.getpixel(shang) == 0:
zuobiao = str(shang[0]) + ',' + str(shang[1])
if zuobiao not in all_pixel_list:
all_pixel_list.append(zuobiao)
pixel_list.append(zuobiao)
check_around(shang[0], shang[1])
if image.getpixel(xia) == 0:
zuobiao = str(xia[0])+','+str(xia[1])
if zuobiao not in all_pixel_list:
all_pixel_list.append(zuobiao)
pixel_list.append(zuobiao)
check_around(xia[0],xia[1])
if image.getpixel(zuo) == 0:
zuobiao = str(zuo[0])+','+str(zuo[1])
if zuobiao not in all_pixel_list:
all_pixel_list.append(zuobiao)
pixel_list.append(zuobiao)
check_around(zuo[0],zuo[1])
if image.getpixel(you) == 0:
zuobiao = str(you[0])+','+str(you[1])
if zuobiao not in all_pixel_list:
all_pixel_list.append(zuobiao)
pixel_list.append(zuobiao)
check_around(you[0],you[1])
if __name__ == '__main__':
path = os.path.dirname(__file__) +'jpg/'
out_path = os.path.dirname(__file__) +'out_jpg/'
for filename in os.listdir(path):
image = Image.open(path + filename)
image = image.convert('L')
id = filename.split('.')[0]
image = Binarization(image)
image.save(out_path + '\{}_二值化.jpg'.format(id))
partition(image)
all_pixel_list.clear()下载验证码程序
# 下载验证码
import requests
import os
path = os.path.dirname(__file__)
for i in range(10):
url = 'https://my.cnki.net/Register/CheckCode.aspx?id=1605429917005'
response = requests.get(url)
file_path = path + 'jpg/{}.jpg'.format(i)
with open(file_path,'wb') as f:
f.write(response.content)
