Example of a random Verification Code (Chinese Verification Code) generated by python

Source: Internet
Author: User
Tags drawtext
This article describes how to generate a Chinese Random verification code in python. For more information, see the following code:


#-*-Coding: UTF-8 -*-
Import Image, ImageDraw, ImageFont
Import random
Import math, string

Class RandomChar ():
"Used to randomly generate Chinese characters """
@ Staticmethod
Def Unicode ():
Val = random. randint (0x4E00, 0x9FBF)
Return unichr (val)

@ Staticmethod
Def GB2312 ():
Head = random. randint (0xB0, 0xCF)
Body = random. randint (0xA, 0xF)
Tail = random. randint (0, 0xF)
Val = (head <8) | (body <4) | tail
Str = "% x" % val
Return str. decode ('hex'). decode ('gb2312 ')

Class ImageChar ():
Def _ init _ (self, fontColor = (0, 0, 0 ),
Size = (100, 40 ),
FontPath = 'wqy. ttc ',
BgColor = (1, 255,255,255 ),
FontSize = 20 ):
Self. size = size
Self. fontPath = fontPath
Self. bgColor = bgColor
Self. fontSize = fontSize
Self. fontColor = fontColor
Self. font = ImageFont. truetype (self. fontPath, self. fontSize)
Self. image = Image. new ('rgb ', size, bgColor)

Def rotate (self ):
Self. image. rotate (random. randint (0, 30), expand = 0)

Def drawText (self, pos, txt, fill ):
Draw = ImageDraw. Draw (self. image)
Draw. text (pos, txt, font = self. font, fill = fill)
Del draw

Def randRGB (self ):
Return (random. randint (0,255 ),
Random. randint (0,255 ),
Random. randint (0,255 ))

Def randPoint (self ):
(Width, height) = self. size
Return (random. randint (0, width), random. randint (0, height ))

Def randLine (self, num ):
Draw = ImageDraw. Draw (self. image)
For I in range (0, num ):
Draw. line ([self. randPoint (), self. randPoint ()], self. randRGB ())
Del draw

Def randChinese (self, num ):
Gap = 5
Start = 0
For I in range (0, num ):
Char = RandomChar (). GB2312 ()
X = start + self. fontSize * I + random. randint (0, gap) + gap * I
Self. drawText (x, random. randint (-5, 5), RandomChar (). GB2312 (), self. randRGB ())
Self. rotate ()
Self. randLine (18)

Def save (self, path ):
Self. image. save (path)

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.