Python generates a verification code using the PIL module

Source: Internet
Author: User

Import Image, Imagedraw, Imagefont, ImageFilter

Import Random


# Random Letters

Def Rndchar ():

Return Chr (Random.randint (65, 90))


# random Color 1

Def rndcolor ():

Return (Random.randint, 255), Random.randint (255), Random.randint (64, 255))


# Random Color 2

Def rndColor2 ():

Return (Random.randint (127), Random.randint (127), Random.randint (32, 127))


# 240 * 60

width = 60 * 4

Height = 60

Image = Image.new (' RGB ', (width, height), (255,255,255))


# Create a Font object

Font = Imagefont.truetype ('/usr/share/fonts-droid/truetype/droidsansfallback.ttf ', 36)


# Create Draw Object

Draw = Imagedraw.draw (image)


# Fill each pixel

For x in range (width):

For y in range (height):

Draw.point ((x, y), Fill=rndcolor ())


# output text

For T in range (4):

Draw.text (* t +10, ten), Rndchar (), Font=font, Fill=rndcolor2 ())


# Blur

Image = Image.filter (Imagefilter.blur)

Image.Save ('/home/godben/code.jpg ', ' jpeg ')

This article is from the "Godben" blog, make sure to keep this source http://godben.blog.51cto.com/8919725/1833846

Python generates a verification code using the PIL module

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.