最近一直在复习,马上要期末考试了,所以这段时间没怎么写博客……今天,正好有时间来写一写博客,希望访问该博客的朋友们点一点赞,谢谢!!
1.绘制图片
生成图片首先要导入PIL模块,"D:\360Downloads\ziti\simhei.ttf"是字体的路径
import PIL
from PIL import Image
from PIL import ImageDraw #画图
from PIL import ImageFont
image=Image.new(mode="RGB",size=(120,30)) #新建一个图片
draw=ImageDraw.Draw(image,mode="RGB")
font=ImageFont.truetype("D:\\360Downloads\\ziti\\simhei.ttf",28)
draw.text([0,0],"123445667","red",font=font)
image.show()
2.随机生成干扰线
画线:
draw.line((0,0,30,50),fill=“red”)
随机生成干扰线:
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=fgcolor,width=3)
3.含数字英文的验证码生成
生成验证码的时候,需要将验证码进行扭曲,然后用ImageFilter处理边界,其次需要生成干扰线,不然验证码很容易被人识别
import random
import math
import string
import sys
from PIL import Image,ImageFont,ImageDraw,ImageFilter
#字体路径
font_path="D:\\360Downloads\\ziti\\georgia.ttf"
#位数
numbers=4
#验证码大小
size=(150,60)
#背景颜色
bgcolor=(255,255,255)
#字体颜色
fgcolor=(0,0,0)
#line 干扰线
draw_line=True
line_numbers=(1,5)
def fgcolorpng():
global fgcolor
color1=random.randint(0,255)
color2=random.randint(0,255)
color3=random.randint(0,255)
fgcolor=(color1,color2,color3)
def make_text():
source1=[str(x) for x in range(0,10)]
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(source1)
source.extend(source2)
source.extend(source3)
return "".join(random.sample(source,numbers))
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=fgcolor,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=fgcolor)
if draw_line:
num=random.randint(1,6)
for i in range(0,num):
make_line(draw,width,height)
image=image.transform((width+20,height+10),Image.AFFINE,(1,-0.2,0.5,-0.2,1,0),Image.BILINEAR) #扭曲
image=image.filter(ImageFilter.EDGE_ENHANCE) #滤镜,处理边界
filename="D:\\360Downloads\\code"+"\\"+text+".png"
with open(filename,'wb') as f:
image.save(f,format='png')
for i in range(1,31):
fgcolorpng()
make_codepng()
找到验证码生成的路径,查看结果:
4.随机生成中文字符
import random
def GBK2312():
head = random.randint(0xb0, 0xf7)
body = random.randint(0xa1, 0xfe)
val = f'{head:x}{body:x}'
str = bytes.fromhex(val).decode('gb2312')
return str
def make_text():
str02=[]
for i in range(1,5):
str01=GBK2312()
str02.append(str01)
print(' '.join(str02))
if __name__ == '__main__':
make_text()
5.中文验证码的生成
生成中文验证码的时候,要选用含有中文的字体
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from PIL import ImageFilter
import random
#图片的尺寸
size=(150,60)
#背景颜色
bgcolor=(255,255,255)
#字体颜色
fgcolor=(0,0,0)
#画线
draw_line=True
#数量
numbers=4
def draw_color():
global fgcolor
fgcolor1=random.randint(0,255)
fgcolor2 = random.randint(0, 255)
fgcolor3 = random.randint(0, 255)
fgcolor=(fgcolor1,fgcolor2,fgcolor3)
"""中文汉字的生成"""
def GBK2312():
head = random.randint(0xb0, 0xf7)
body = random.randint(0xa1, 0xfe)
val = f'{head:x}{body:x}'
str = bytes.fromhex(val).decode('gb2312','ignore')
return str
def make_text():
str02=[]
for i in range(1,5):
str01=GBK2312()
str02.append(str01)
return ''.join(str02)
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=fgcolor,width=3)
def draw_image():
width,height=size
image=Image.new("RGBA",(width,height),bgcolor)
draw=ImageDraw.Draw(image)
text=make_text()
filepath="D:\\360Downloads\\ziti\\simkai.ttf"
font=ImageFont.truetype(filepath,40)
font_width,font_height=font.getsize(text)
draw.text(((width-font_width)/numbers,(height-font_height)/2),text,fill=fgcolor,font=font)
if draw_line:
num=random.randint(0,6)
for i in range(0,num):
make_line(draw,width,height)
image=image.transform((width+20,height+10),Image.AFFINE,(1,-0.2,0.5,-0.2,1,0.5),Image.BILINEAR)
image=image.filter(ImageFilter.EDGE_ENHANCE)
filename="D:\\360Downloads\\code\\"+text+".png"
with open(filename,'wb') as f:
image.save(f,format='png')
if __name__ == '__main__':
for i in range(1,51):
draw_color()
draw_image()
这样,多种多样的验证码就生成了!!!