Python seventh generates random verification code

Source: Internet
Author: User


From PIL import Image
From PIL import Imagedraw
From PIL import Imagefont
From Pylab Import *
Import random,numpy,string

Path = "G:\python file"

Text = Random.sample (string.ascii_letters,4)
#random. Sample is a random fragment of a specified length that can be intercepted from a specified sequence.
#string. Letters is the creation of English letters
Print (text)

# Generate-D random array three-dimensional
Rawarray = Numpy.zeros ((100,300,3), dtype=numpy.uint8) #uint8无符号8位整型
SH = rawarray.shape
For I in range (Sh[0]):
For j in Range (Sh[1]):
for k in range (Sh[2]):
Rawarray[i][j][k]=random.randint (0,255)

# generate the background pic from-array
im = Image.fromarray (rawarray) #将数组转化为图像
Draw = Imagedraw.draw (IM)

# Add check code onto the background
For I in range (len (text)): #4次
Draw.text (75*i+random.randint (0,40), Random.randint (0,40)), Text[i],
Font=imagefont.truetype ("Stxingka. TTF ", 80),
Fill = (Random.randint (0,255), Random.randint (0,255), Random.randint (0,255)))
#第一个参数指定绘制的起始点 (where the upper-left corner of the text is), the second parameter specifies the text content, the third parameter specifies the color of the text,
# The fourth parameter specifies a font (defined by the Imagefont class)
Im.save (path+ "/checkcode.jpg")

Summary: In fact, there is a part of the code I do not understand, that is, about the array, I have not in-depth understanding, but the general check the meaning of each parameter. Here again to use the PIL is about the image of the drawing, visible, this is also an important place, need to master.

Python seventh generates random verification code

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.