编写验证码训练集

详情请看:H:\python学习资料\爬虫\爬虫学习资料\尹成-爬虫--详细\SZday17
1.数字验证码:
#coding:utf-8
import random
import string
import sys
import math
from PIL import ImageFont,ImageDraw,ImageFilter,Image
# 字体路径
font_path= r"H:\python 学习资料 \ 爬虫 \ 爬虫学习资料 \ 尹成 - 爬虫 -- 详细 \SZday17\font\simkai.ttf"
# 位数
numbers= 4
# 验证码大小
size=( 150 , 60 )
# 背景颜色
bgcolor=( 255 , 255 , 255 )
# 字体颜色
fontcolor=( 0 , 0 , 0 )
#line 干扰线
draw_line= True
# 线数量范围
line_numbers=( 1 , 5 )

# 随机背景颜色
def bgfontrange():
global bgcolor
color1=random.randint( 0 , 255 )
color2 = random.randint( 0 , 255 )
color3 = random.randint( 0 , 255 )
bgcolor=(color1,color2,color3)

# 随机字体颜色
def ftfontrange():
global fontcolor
color1=random.randint( 0 , 255 )
color2 = random.randint( 0 , 255 )
color3 = random.randint( 0 , 255 )
fontcolor=(color1,color2,color3)

# 随机生成四个随机数 :
def make_text():
source1= [ str (x) for x in range ( 0 , 10 )] #0-9 之间
source2=[ "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" , "j" , "k" , "l" , "m" , "n" , "o" , "p" , "q" , "r" , "s" , "t" , "u" , "v" , "w" , "x" , "y" , "z" ]
source3=[ "A" , "B" , "C" , "D" , "E" , "F" , "G" , "H" , "I" , "J" , "K" , "L" , "M" , "N" , "O" , "P" , "Q" , "R" , "S" , "T" , "U" , "V" , "W" , "X" , "Y" , "Z" ]
source=[]
source.extend(source3)
source.extend(source1)
source.extend(source2)
return "" .join(random.sample(source,numbers)) #"", 加上 4 个随机数

# 随机划线
def make_line(draw,width,height):
begin=(random.randint( 0 ,width),random.randint( 0 ,height))
end = (random.randint( 0 ,width), random.randint( 0 , height))
draw.line([begin,end], fill =fontcolor, width = 3 ) # 绘线

# 生成验证码
def make_codepng():
width,height=size # 图片的宽度与高度
# RGBA 是透明度
image= Image.new( "RGBA" ,(width,height),bgcolor) # 创建图片
draw=ImageDraw.Draw(image) # 绘图工具
text=make_text() # 生成随机字符串
font=ImageFont.truetype(font_path, 40 ) # 定义字体大小
font_width,font_height=font.getsize(text) # 设置字体的宽度与高度
draw.text( ((width-font_width)/ 2 ,(height-font_height)/ 2 ), # 写入了文字
text,
font =font,
fill =fontcolor) # 写入文字

if draw_line:
print ( "make_line" )
num=random.randint( 1 , 6 )
for i in range (num):
make_line(draw,width,height)

num1 = random.uniform( 0.9 , 1 )
num2 = random.uniform(- 0.3 , 0 )
num3 = random.uniform(- 0.1 , 0.1 )
num4 = random.uniform(- 0.3 , 0.1 )
num5 = random.uniform( 0.8 , 1 )
num6 = random.uniform( 0 , 0.2 ) # 生成小数, min,max

image=image.transform((width+ 30 ,height+ 20 ),
Image.AFFINE,
(num1,num2,num3,num4,num5,num6),
Image.BILINEAR) # 文字扭曲
image=image.filter(ImageFilter.EDGE_ENHANCE_MORE) # 处理边界
# image.show()
filename= r"H:\python 学习资料 \ 爬虫 \ 爬虫学习资料 \ 尹成 - 爬虫 -- 详细 \SZday17\code\ 生成验证码 \code8" + " \\ " +text+ ".png"
with open (filename, "wb" ) as file:
image.save(file, format = "png" )


for i in range ( 100 ):
bgfontrange() # 背景颜色随机
ftfontrange() # 字体随机颜色
make_codepng()





2.汉字随机生成:
#coding:utf-8
import random
import string
import sys
import math
from PIL import ImageFont,ImageDraw,ImageFilter,Image
# 字体路径
font_path= r"C:\Users\Tsinghua-yincheng\Desktop\data\simkai.ttf"
# 位数
numbers= 4
# 验证码大小
size=( 150 , 60 )
# 背景颜色
bgcolor=( 255 , 255 , 255 )
# 字体颜色
fontcolor=( 0 , 0 , 0 )
#line 干扰线
draw_line= True
line_numbers=( 1 , 5 )

# 中文字符
class RandomChar():
# 方法一 :
@staticmethod
def Unicode():
val = random.randint( 0x4E00 , 0x9FA5 ) # 基本汉字的 unicode 编码范围 (0x4E00, 0x9FA5)
return chr (val)
# 方法二
@staticmethod
def GB2312():
# 范围 :(0xb0, 0xf7) , 可以将范围缩小 , 因为太多生僻字
head = random.randint( 0xb0 , 0xba )
body = random.randint( 0xa1 , 0xf9 ) # head 区号为 55 的那一块最后 5 个汉字是乱码 , 为了方便缩减下范围
val = f'{head:x}{body:x}'
str = bytes .fromhex(val).decode( 'gb2312' )
return str


def make_text():
text= ""
for i in range ( 4 ):
text+=RandomChar.GB2312()
return text #"", 加上 4 个随机数

def bgfontrange():
global bgcolor
color1=random.randint( 0 , 255 )
color2 = random.randint( 0 , 255 )
color3 = random.randint( 0 , 255 )
bgcolor=(color1,color2,color3)
def ftfontrange():
global fontcolor
color1=random.randint( 0 , 255 )
color2 = random.randint( 0 , 255 )
color3 = random.randint( 0 , 255 )
fontcolor=(color1,color2,color3)


#print make_text() 随机划线
def make_line(draw,width,height):
begin=(random.randint( 0 ,width),random.randint( 0 ,height))
end = (random.randint( 0 ,width), random.randint( 0 , height))
draw.line([begin,end], fill =fontcolor, width = 3 ) # 绘线
# 生成验证码
def make_codepng():
width,height=size # 图片的宽度与高度
image= Image.new( "RGBA" ,(width,height),bgcolor) # 创建图片
draw=ImageDraw.Draw(image) # 绘图工具
text=make_text() # 生成随机字符串
font=ImageFont.truetype(font_path, 40 ) # 字体
font_width,font_height=font.getsize(text) # 字体的宽度与高度
draw.text( ((width-font_width)/numbers,(height-font_height)/ 2 ), # 写入了文字
text,
font =font,
fill =fontcolor) # 写入文字

if draw_line:
print ( "make_line" )
num=random.randint( 1 , 6 )
for i in range (num):
make_line(draw,width,height)
#(1,-0.1,0,-0.2,0.9,0)
num1=random.uniform( 0.9 , 1 )
num2 = random.uniform(- 0.1 , 0.1 )
num3 = random.uniform(- 0.1 , 0.1 )
num4 = random.uniform( 0 , 0.1 )
num5 =random.uniform( 0.8 , 1 )
num6 = random.uniform( 0 , 0.2 ) # 生成小数, min,max

image=image.transform((width+ 30 ,height+ 20 ),
Image.AFFINE,
(num1,num2,num3,num4,num5,num6),
Image.BILINEAR) # 扭曲
image=image.filter(ImageFilter.EDGE_ENHANCE_MORE) # 处理边界
filename= r"H:\python 学习资料 \ 爬虫 \ 爬虫学习资料 \ 尹成 - 爬虫 -- 详细 \SZday17\code\ 生成验证码 \code7" + " \\ " +text+ ".png"
with open (filename, "wb" ) as file:
image.save(file, format = "png" )

for i in range ( 100 ):
bgfontrange()
ftfontrange()
make_codepng()

你可能感兴趣的:(python基础类)