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