Python 随机生成中文验证码的实例代码


Posted in Python onMarch 20, 2013

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中dir函数用法分析
Apr 17 Python
itchat和matplotlib的结合使用爬取微信信息的实例
Aug 25 Python
python字符串常用方法
Jun 14 Python
Django项目开发中cookies和session的常用操作分析
Jul 03 Python
对PyTorch torch.stack的实例讲解
Jul 30 Python
python3.6编写的单元测试示例
Aug 17 Python
使用pyinstaller逆向.pyc文件
Dec 20 Python
pytorch 自定义参数不更新方式
Jan 06 Python
关于初始种子自动选取的区域生长实例(python+opencv)
Jan 16 Python
python实现简单飞行棋
Feb 06 Python
解决tensorflow添加ptb库的问题
Feb 10 Python
Python实现敏感词过滤的4种方法
Sep 12 Python
python 字符串格式化代码
Mar 17 #Python
Python中条件选择和循环语句使用方法介绍
Mar 13 #Python
python list 合并连接字符串的方法
Mar 09 #Python
python的正则表达式re模块的常用方法
Mar 09 #Python
Python语言编写电脑时间自动同步小工具
Mar 08 #Python
py2exe 编译ico图标的代码
Mar 08 #Python
python中wx将图标显示在右下角的脚本代码
Mar 08 #Python
You might like
适用于php-5.2 的 php.ini 中文版[金步国翻译]
2011/04/17 PHP
PHP防CC攻击实现代码
2011/12/29 PHP
thinkphp 多表 事务详解
2013/06/17 PHP
浅析SVN常见问题及解决方法
2013/06/21 PHP
PHP异常处理定义与使用方法分析
2017/07/25 PHP
PHP文件管理之实现网盘及压缩包的功能操作
2017/09/20 PHP
js操作Xml(向服务器发送Xml,处理服务器返回的Xml)(IE下有效)
2009/01/30 Javascript
jquery阻止后续事件只执行第一个事件
2014/07/24 Javascript
jquery实现列表上下移动功能
2016/02/25 Javascript
JS验证逗号隔开可以是中文字母数字
2016/04/22 Javascript
jquery+Jscex打造游戏力度条
2020/09/12 Javascript
微信小程序icon组件使用详解
2018/01/31 Javascript
使用node.js实现微信小程序实时聊天功能
2018/08/13 Javascript
详解JS转换数值函数Number()、parseInt()、parseFloat()
2018/08/24 Javascript
Vue 使用计时器实现跑马灯效果的实例代码
2019/07/11 Javascript
Vue中使用matomo进行访问流量统计的实现
2019/11/05 Javascript
[01:42]辉夜杯战队访谈宣传片—FANTUAN
2015/12/25 DOTA
Python的Bottle框架的一些使用技巧介绍
2015/04/08 Python
深入浅出分析Python装饰器用法
2017/07/28 Python
python调用Delphi写的Dll代码示例
2017/12/05 Python
Python 中导入csv数据的三种方法
2018/11/01 Python
在linux下实现 python 监控usb设备信号
2019/07/03 Python
Python倒排索引之查找包含某主题或单词的文件
2019/11/13 Python
python实现画循环圆
2019/11/23 Python
利用python画出AUC曲线的实例
2020/02/28 Python
纯CSS3实现鼠标悬停提示气泡效果
2014/02/28 HTML / CSS
css3实现文字扫光渐变动画效果的示例
2017/11/07 HTML / CSS
详解移动端html5页面长按实现高亮全选文本内容的兼容解决方案
2016/12/03 HTML / CSS
Reebok官方旗舰店:美国知名健身品牌锐步
2019/01/07 全球购物
Marlies Dekkers内衣荷兰官方网店:荷兰奢侈内衣品牌
2020/03/27 全球购物
高中生学习生活的自我评价
2013/11/27 职场文书
公安局班子个人对照检查材料思想汇报
2014/10/09 职场文书
文明倡议书
2015/01/19 职场文书
拾金不昧表扬稿大全
2015/05/05 职场文书
钱学森观后感
2015/06/04 职场文书
mongodb的安装和开机自启动详细讲解
2021/08/02 MongoDB