Python generates a verification code

Source: Internet
Author: User

#encoding =utf-8import random# Import matplotlib.pyplot as Pltimport stringimport sysimport mathfrom PIL Import Image,imag Edraw,imagefont,imagefilterfilename= "c:/users/123/desktop/yzm/" #字体的位置, different versions of the system will have different Buxtonsketch.ttffont_path = ' C :/windows/fonts/georgia.ttf ' #font_path = ' c:/windows/fonts/. ttf ' #生成几位数的验证码number = 4# the height and width of the generated captcha image size = ( 129,53) #背景颜色, the default is white bgcolor = (255,255,255) #字体颜色, the default is blue FontColor = (0,0,0) #干扰线颜色.  The default is red LineColor = (0,0,0) #是否要加入干扰线draw_line = true# Add the upper and lower bounds of the number of interfering lines line_number = (1,5) #用来随机生成一个字符串def gene_text (): # Source = List (string.letters) # for index in range (0,10): # source.append (str (index)) Source = [' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', '    5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 '] # source = [' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', # ' S ', ' T ', ' U ', ' V ', ' W ', ' Z ', ' X ', ' Y '] return '. Join (Random.sample (Source,number)) #number是生成验证码的位数 # used to draw the interference line def gene_line (draw,width,height): # begin = (Random.randint (0, width), random.randint (0, height)) # end = (Random.randint (0, width), random.randint (0, height)) begin = (0, random.randint (0, height)) End = (Random.randint (0, height)) draw.line ([begin, end], fill = linecolor,width=3) #生成验证码def gene_code (): Width, Height = size #宽和高 image = Image.new (' RGBA ', (width,height), bgcolor) #创建图片 font = Imagefont.truetype (font_path,40) #验    The font of the code draw = Imagedraw.draw (image) #创建画笔 text = Gene_text () #生成字符串 font_width, font_height = font.getsize (text) Draw.text (((width-font_width)/number, (Height-font_height)/number), text, font= Font,fill=fontcolor)  #填充字符串 if Draw_line:gene_line (draw,width,height) image = Image.transform ((width+30,height+10), Image.affine, (1,-0.3,0,-0.1,1,0), image.bilinear) #创建扭曲 # Image = Image.transform ((width+20,height+10), Image.affine, (1,-0.3,0,-0. 1,1,0), image.bilinear) #创建扭曲 Image = Image.filter (imagefilter.edge_enhance_more) #滤镜, boundary strengthening # a = str (m) AA = STR ( ". png") path = FilenaMe + text + AA # cv2.imwrite (path, I1) # image.save (' idencode.jpg ') #保存验证码图片 image.save (path) x=1# if __name__ = = "__main__": # for K in (1,1000): While X<20:gene_code () x+=1

  

Python generates a verification code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.