python代码
复制代码代码如下:
# -*- coding: utf-8 -*-
import Image,ImageDraw,ImageFont
import random
import math, string
class RandomChar():
"""用于随机生成汉字"""
@staticmethod
def Unicode():
val = random.randint(0x4E00, 0x9FBF)
return unichr(val)
@staticmethod
def GB2312():
head = random.randint(0xB0, 0xCF)
body = random.randint(0xA, 0xF)
tail = random.randint(0, 0xF)
val = ( head << 8 ) | (body << 4) | tail
str = "%x" % val
return str.decode('hex').decode('gb2312')
class ImageChar():
def __init__(self, fontColor = (0, 0, 0),
size = (100, 40),
fontPath = 'wqy.ttc',
bgColor = (255, 255, 255),
fontSize = 20):
self.size = size
self.fontPath = fontPath
self.bgColor = bgColor
self.fontSize = fontSize
self.fontColor = fontColor
self.font = ImageFont.truetype(self.fontPath, self.fontSize)
self.image = Image.new('RGB', size, bgColor)
def rotate(self):
self.image.rotate(random.randint(0, 30), expand=0)
def drawText(self, pos, txt, fill):
draw = ImageDraw.Draw(self.image)
draw.text(pos, txt, font=self.font, fill=fill)
del draw
def randRGB(self):
return (random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255))
def randPoint(self):
(width, height) = self.size
return (random.randint(0, width), random.randint(0, height))
def randLine(self, num):
draw = ImageDraw.Draw(self.image)
for i in range(0, num):
draw.line([self.randPoint(), self.randPoint()], self.randRGB())
del draw
def randChinese(self, num):
gap = 5
start = 0
for i in range(0, num):
char = RandomChar().GB2312()
x = start + self.fontSize * i + random.randint(0, gap) + gap * i
self.drawText((x, random.randint(-5, 5)), RandomChar().GB2312(), self.randRGB())
self.rotate()
self.randLine(18)
def save(self, path):
self.image.save(path)
调用方法
复制代码代码如下:
ic = ImageChar(fontColor=(100,211, 90))
ic.randChinese(4)
ic.save("1.jpeg")
您可能感兴趣的文章:
python 图片验证码代码分享
python 图片验证码代码
python为tornado添加recaptcha验证码功能
QQ空间 搜狐微博 人人网 开心网 百度搜藏更多
Tags:Python 随机 中文 验证码
复制链接收藏本文打印本文关闭本文返回首页
上一篇:python 字符串格式化代码
下一篇:windows下wxPython开发环境安装与配置方法
相关文章
2014-06-06Python程序设计入门(3)数组的使用
2013-12-12Python yield使用方法示例
2014-01-01压缩包密码破解示例分享(类似典破解)
2014-03-03python实现ip查询示例
2014-02-02Python操作列表的常用方法分享
2008-09-09Python translator使用实例
2012-05-05Python使用Socket(Https)Post登录百度的实现代码
2014-02-02python实现倒计时的示例
2014-04-04python实现数通设备tftp备份配置文件示例
2013-12-12Python使用urllib2获取网络资源实例讲解
文章评论
最 近 更 新
python实现系统状态监测和故障转移实例方
精确查找PHP WEBSHELL木马的方法(1)
python转换摩斯密码示例
python类型强制转换long to int的代码
python计数排序和基数排序算法实例
python开发的小球完全弹性碰撞游戏代码
python计算最大优先级队列实例
从零学python系列之数据处理编程实例(一
Python BeautifulSoup中文乱码问题的2种解
python和C语言混合编程实例
热 点 排 行
Python入门教程 超详细1小时学会
python 中文乱码问题深入分析
比较详细Python正则表达式操作指
Python字符串的encode与decode研
Python open读写文件实现脚本
Python enumerate遍历数组示例应
Python 深入理解yield
Python+Django在windows下的开发
python 文件和路径操作函数小结
python 字符串split的用法分享