修改自另一位网友的python校验码,原本是用于Django的。因为最近学习web.py的原因,就稍作修改了。
这里还需要添加session的处理
#coding=utf-8
import random, Image, ImageDraw, ImageFont, md5, datetime, ImageColor, StringIO
# get random color
def get_color():
colors = ['Black','Orange','Red','Brown','DarkBlue','Purple','DarkCyan','DarkBlue']
return ImageColor.getrgb(colors[random.randrange(1,9)-1])
# get random font-size
def get_font():
return ImageFont.truetype("arial.ttf", random.randrange(15,25,5))
# make code image
def make_check_code_image(image=''):
color = ImageColor.getrgb('white')
#im = Image.open(image)
im = Image.new('RGB',(60,20), color)
draw = ImageDraw.Draw(im)
mp = md5.new()
mp_src = mp.update(str(datetime.datetime.now()))
mp_src = mp.hexdigest()
rand_str = mp_src[0:4]
#print rand_str
color = ImageColor.getrgb('LightGray')
for i in range(200):
x = random.randrange(1,60)
y = random.randrange(1,20)
draw.point((x, y), fill=color)
draw.text((5,1), rand_str[0], fill=get_color(), font=get_font())
draw.text((15,1), rand_str[1], fill=get_color(), font=get_font())
draw.text((30,1), rand_str[2], fill=get_color(), font=get_font())
draw.text((45,1), rand_str[3], fill=get_color(), font=get_font())
draw.line((0,10,60,15), fill=get_color())
del draw
#request.session['checkcode'] = rand_str
#print request.session['checkcode']
buf = StringIO.StringIO()
im.save(buf, 'gif')
buf.closed
if image: im.save(image)
return buf.getvalue()
if __name__=='__main__':
make_check_code_image('test.gif')
以下是引用:
class checkcode:
def GET(self):
web.header("Content-Type",'image/gif')
return make_check_code_image()