question No. 0010: use Python to generate an image similar to the letter code in
Idea: First randomly generate a verification code, and then use the Python PiL library to draw the image of the activation code, the specific point is to create a canvas, add verification code word up, add noise to the interference, and then fuzzy processing, and then saved to the name of the verification code in the picture.
0010. Generate a captcha picture. py
#!/usr/bin/env python#coding: Utf-8ImportImage, Imagedraw, Imagefont, ImageFilterImportstring, Randomfontpath ="/usr/share/fonts/truetype/ttf-devanagari-fonts/"# Get a random four letters def Getrandomchar(): return[Random.choice (String.letters) for_inchRange4)]# Get Color def getrandomcolor(): return(Random.randint ( -, -), Random.randint ( -, -), Random.randint ( -, -))# Get Verification code picture def getcodepiture():width = -Height = - # Create CanvasImage = Image.new (' RGB ', (width, height), ( the, the, the)) Font = Imagefont.truetype (Fontpath +' Kalimati.ttf ', +) Draw = Imagedraw.draw (image)# Create Authenticode objectCode = Getrandomchar ()# Put the verification on the canvas forTinchRange4): Draw.text (( -* t +Ten,0), Code[t], Font=font, Fill=getrandomcolor ())# Fill the noise point for_inchRange (Random.randint ( the, the): Draw.point ((Random.randint (0, width), random.randint (0, height)), Fill=getrandomcolor ())# Fuzzy ProcessingImage = Image.filter (Imagefilter.blur)# Save a picture with the name verification codeImage.Save ("". Join (Code) +'. jpg ',' JPEG ');if__name__ = =' __main__ ': Getcodepiture ()
Two randomly generated CAPTCHA images are selected:
Python show-me-the-code No. 0010 generate captcha picture