Python Show-me-the-code The No. 0010 question generate a captcha picture

Source: Internet
Author: User

question No. 0010: use Python to generate an image similar to the letter code in

    • Reading materials

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

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.