python PIL库生成验证码

代码来自山东大学经济学院韩振老师,请勿转载


'''Perhaps you need to use pillow, a replacement of PIL.'''
from PIL import Image, ImageDraw, ImageFont
import random
import string

#all possible characters to show in the picture
characters = string.ascii_letters+string.digits

#get the characters of certain length to show in the picture
def selectedCharacters(length):
    '''length:the number of characters to show'''
    result = ""
    for i in range(length):
        result += random.choice(characters)
    return result

def getColor():
    '''get a random color'''
    r = random.randint(0,255)
    g = random.randint(0,255)
    b = random.randint(0,255)
    return (r,g,b)

def main(size=(200,100), characterNumber=6, bgcolor=(255,255,255)):
    imageTemp = Image.new('RGB', size, bgcolor)
    font = ImageFont.truetype('c:\\windows\\fonts\\TIMESBD.TTF', 48)  #24 is the font size
    draw = ImageDraw.Draw(imageTemp)
    text = selectedCharacters(characterNumber)
    width, height = draw.textsize(text, font)
    #draw the selected characters
    offset = 2
    for i in range(characterNumber):
        offset += width//characterNumber
        position = (offset, (size[1]-height)//2+random.randint(-10,10))
        draw.text(xy=position, text=text[i], font=font, fill=getColor())    
    #make some transform to the picture, using simple point operation here
    imageFinal = Image.new('RGB', size, bgcolor)
    pixelsFinal = imageFinal.load()
    pixelsTemp = imageTemp.load()
    for y in range(0, size[1]):
        offset = random.randint(-1,1)
        for x in range(0, size[0]):
            newx = x+offset
            if newx>=size[0]:
                newx = size[0]-1
            elif newx<0:
                newx = 0
            pixelsFinal[newx,y] = pixelsTemp[x,y]
    draw = ImageDraw.Draw(imageFinal)
    #draw some noise points
    for i in range(int(size[0]*size[1]*0.07)):
        draw.point((random.randint(0,size[0]), random.randint(0,size[1])), fill=getColor())

    #draw some noise lines
    for i in range(8):
        start = (0, random.randint(0, size[1]-1))
        end = (size[0], random.randint(0, size[1]-1))
        draw.line([start, end], fill=getColor(), width=1)

    #draw some noise arcs
    for i in range(8):
        start = (-50, -50)
        end = (size[0]+10, random.randint(0, size[1]+10))
        draw.arc(start+end, 0, 360, fill=getColor())
    #save the piture
    imageFinal.save("result.jpg")
    imageFinal.show()

if __name__=="__main__":
    main((200,100), 8, (255,255,255))
                   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值