对于ubuntu 12.04LST 安装PIL (https://github.com/sloonz/pil-py3k)
需要:
说明(
http://pillow.readthedocs.org/en/latest/installation.html#linux-installation)
libjpeg8
libjpeg8-dev
libfreetype6
libfreetype6-dev
zlib1g-dev
libtiff4-dev
liblcms1-dev
libwebp-dev
tcl8.5-dev
tk8.5-dev
一定要等所有所需模块装好 并且 看下这个(
http://jj.isgeek.net/2011/09/install-pil-with-jpeg-support-on-ubuntu-oneiric-64bits/)
然后再装PIL
再贴下网上有好心人写的 生成验证码的例子
#Fly web app - Verification Code
# -*- coding: utf-8 -*-
import random
from io import BytesIO
import Image, ImageDraw, ImageFont
# 特别注意 from PIL import Image 和 import Image的区别 由于PIL包都是import xxx 所以我们不能写成from xxx import yyy
class VCode:
def __init__(self,size=(100,30),length=4,font='',
font_color=(51,102,204),font_size=25,font_bg=(255,255,255),
line=(3,5),pointBorder = (50,47),save_type= 'JPEG',quality=100):
"""
验证码生成类
参数:
size - 图片高宽(width,height)
length - 验证码长度
font - 字体路径
font_color - 字体颜色(允许是: #FF0000 或 RGB)
font_size - 字体大小
font_bg - 背景颜色
line - 干扰线 (最少,最大)
pointBorder - 散点(越大越多,越大越少)
save_type - 保存类型
quality - 品质
"""
self._size = size
self._font = font
self._font_color = self._str_hex(font_color)
self._font_size = font_size
self._length = length
self._font_bg = self._str_hex(font_bg)
self._save_type = save_type
self._quality = quality
#初始化
self._im = None
self._draw = None
self._code = ''
self._char = 'abcdefghkmnprstuvwyzABCDEFGHKLMNPRSTUVWYZ23456789'
self._pointBorder = pointBorder
self._line = line
def Generate(self):
"""生成验证码,并返回图像内容,和验证码"""
#生成code
random_code = self._randomCode()
self._code = '%s' % ''.join(random_code)
code_str = '%s' % ' '.join(random_code)
#图像对象
self._im = Image.new('RGB',self._size,self._font_bg)
self._draw = ImageDraw.ImageDraw(self._im)
imFont = ImageFont.truetype(self._font,self._font_size)
w,h = imFont.getsize(self._code)
xy = ((self._size[0]-w)/2,(self._size[1]-h)/2)
self._draw.text(xy,code_str,fill=self._font_color,font=imFont)
#画干扰点
self._createPoints()
#画干扰线
self._createLines()
#file对象
fp = BytesIO()
#保存图像
self._im.save(fp,self._save_type,quality=self._quality)
return fp.getvalue(),self._code
def _str_hex(self,color):
"""转换颜色值为RGB"""
if type(color) is str:
color = color.replace('#','')
temp_color = (int(color[0:2],16),int(color[2:4],16),int(color[4:6],16))
else:
temp_color = color
return temp_color
def _randomCode(self):
"""生成随机code """
return random.sample(self._char,self._length)
def _createPoints(self):
"""画点"""
for x in range(self._size[0]):
for y in range(self._size[1]):
flag = random.randint(0, self._pointBorder[0])
if flag > self._pointBorder[1]:
self._draw.point((x, y), fill = self._randomRGB())
del flag
def _createLines(self):
"""画干扰线"""
lineNum = random.randint(self._line[0], self._line[1])
for i in range(lineNum):
begin = (random.randint(0, self._size[0]),
random.randint(0, self._size[1])
)
end = (random.randint(0, self._size[0]),
random.randint(0, self._size[1])
)
self._draw.line([begin, end], fill = self._randomRGB())
def _randomRGB(self):
"""随机生成RGB"""
return (random.randint(0,255),random.randint(0,255),random.randint(0,255))
if __name__ == "__main__":
verification = VCode(font='font/ARLRDBD.TTF')
im, code = verification.Generate()
with open("test.jpeg", "wb") as f:
f.write(im)
print(code)
用tornado render html是 html里的python 代码
Tornado的模板支持控制语句和表达语句,控制语句是使用 {% 和 %} 包起来的,例如 {% if len(items) > 2 %} 。表达语句是使用 {{ 和 }} 包起来的,例如 {{ items[0] }} 。
控制语句和对应的Python语句的格式基本完全相同。我们支持 if 、 for 、 while 和 try ,这些语句逻辑结束的位置需要用 {% end %} 做标记
图片内容无需保存到.jpeg中, 具体参考下面
http://en.wikipedia.org/wiki/Data_URI_scheme#HTML
是用mako来在html嵌套python代码
http://blog.csdn.net/luanruisong/article/details/9414635