Python randomly generates the instance code of the Chinese verification code. if you need it, refer to the python code.
The code is as follows:
#-*-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)
Call method
The code is as follows:
Ic = ImageChar (fontColor = (100,211, 90 ))
Ic. randChinese (4)
Ic. save ("1.jpeg ")