Python generates CAPTCHA script

Source: Internet
Author: User
Tags image processing library

Recently, a small script is written in Python every day, practicing the use of Python syntax.

Verification Code Generation:

Here used the Python image processing library pil, installation pil the process of a small trouble, on the use of Pillow-win32 a file, specific I also forgot, can Baidu under.

Look directly at the code:

#-*-coding:utf-8-*-from PIL import Image,imagefont,imagedraw, Imagefilterimport random# return random letter Def charrandom (): Return Chr ((Random.randint (65,90))) #返回随机数字def Numrandom (): Return R Andom.randint (0,9) #随机颜色def colorRandom1 (): Return (Random.randint (64,255), Random.randint (64,255), Random.randint ( 64,255)) #随机长生颜色2def colorRandom2 (): Return (Random.randint (32,127), Random.randint (32,127), Random.randint (32,127)) width = 4height = 60image = Image.new (' RGB ', (width,height), (255,255,255)); #创建font对象font = Imagefont.truetype (' Arial . TTF ', $); #创建draw对象draw = Imagedraw.draw (image) #填充每一个颜色for x in range (width): A-y in range (height): draw.point ((x, y), fill=colorrandom1 ()) #输出文字for T in range (4): Draw.text ((60*t+10,10), Charrandom (), Font=font, Fill=colorr Andom2 ()) #模糊image = Image.filter (imagefilter.blur) Image.Save ('./image/code.jpg ', ' jpeg ') 

PIL for image processing is still possible:

Here is the Official document: http://effbot.org/imagingbook/is very detailed.

The image class is a very important class in the PIL library, which can be used to create an instance with three methods, such as loading image files directly, reading processed images, and images obtained by fetching. You can perform simple operations such as rotating, cropping, zooming in and out, filtering, and so on.

The Imagefont module is a font module that allows you to set fonts

The idea of the above program:

1, the definition of a number of random get color and digital letters of the function, to obtain the text of the Verification code

2, image.new generate a black canvas, that is, the picture, size 60*240

3. Imagefont defines the fonts that will be used on the image

4. Create a draw object for the picture, and through the draw object, change the pixel of the image and use the color that grows

5, in the picture to produce text, the text also uses the random color

6, the image of the fuzzy processing

7. Save the picture

Finally, you can get a picture of code.jpg under the image file

Extended:

If you can, you can also use the draw object to randomly generate a few lines on the picture, the verification code becomes more ugly understand, you can tilt the text and so on.

Image.rotate (Random.randint (0,30), expand=0) def randomline (self, num): Draw = Imagedraw.draw (self.image) for I in range ( 0,num): Draw.line ([Self.randompoint (), Self.randompoint ()], Self.randomrgb ()) Del Draw

  

About the verification code there are many can be worth seeing, before using OPENCV, just want to do a verification code recognition program, so that can realize the automatic login system, do not need to manually enter the verification code every time, this seems to be part of Turing Research, is also a distinguishing mark between man and Machine, Automatic distinguishing between computer and human Turing Test (English: Completely Automated public Turing test to tell Computers and humans Apart, abbreviated as CAPTCHA), commonly known as CAPTCHA. The specific can be Baidu understand.

Python generates CAPTCHA script

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.