1.python Code writing (random Verification Code):
1 #Coding:utf-82 3 ImportImage, Imagedraw, Imagefont, ImageFilter4 Importstring, Random5 6Fontpath ="/home/itcast/ace/media/"7 8 #get a random four letters9 defGetrandomchar ():Ten return[Random.choice (String.letters) for_inchRange (4)] One A #Get Color - defGetrandomcolor (): - return(Random.randint (+), Random.randint (30, 100), Random.randint ()) the - #Get Verification Code picture - defgetcodepiture (): -width = 240 +Height = 60 - + #Create a canvas AImage = Image.new ('RGB', (width, height), (180,180,180)) atFont = Imagefont.truetype (Fontpath +'Simhei.ttf', 80) -Draw =Imagedraw.draw (image) - - #Create a Captcha object -Code = Getrandomchar ()#code-> [X,a,y,u] - in #put the verification on the canvas - forTinchRange (4): toDraw.text (* t + 0), code[t], Font=font, fill=Getrandomcolor ()) + - #Fill Noise points the for_inchRange (Random.randint (1500,3000)): *Draw.point (Random.randint (0,width), Random.randint (0,height)), fill=Getrandomcolor ()) $ Panax Notoginseng #Blur processing - #image = Image.filter (Imagefilter.blur) the + #save a picture with the name verification code A #code = [x, y, U,a]-xyua.jpg theImage.Save ("". Join (Code) +'. jpg','JPEG'); + - $ if __name__=='__main__': $Getcodepiture ()
Python code build verification Code