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 相关文章推荐
编写同时兼容Python2.x与Python3.x版本的代码的几个示例
Mar 30 Python
浅析Python中的序列化存储的方法
Apr 28 Python
python实现基本进制转换的方法
Jul 11 Python
用python实现将数组元素按从小到大的顺序排列方法
Jul 02 Python
对python多线程与global变量详解
Nov 09 Python
python实现电子产品商店
Feb 26 Python
python和c语言的主要区别总结
Jul 07 Python
python模拟键盘输入 切换键盘布局过程解析
Aug 15 Python
解决Tensorboard 不显示计算图graph的问题
Feb 15 Python
Python Flask上下文管理机制实例解析
Mar 16 Python
Python如何批量获取文件夹的大小并保存
Mar 31 Python
如何在python中实现线性回归
Aug 10 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的面试题集
2006/11/19 PHP
PHP教程 预定义变量
2009/10/23 PHP
php stream_get_meta_data返回值
2013/09/29 PHP
php列出mysql表所有行和列的方法
2015/03/13 PHP
PHP模板引擎Smarty内建函数foreach,foreachelse用法分析
2016/04/11 PHP
PHPMailer发送邮件
2016/12/28 PHP
php设计模式之原型模式分析【星际争霸游戏案例】
2020/03/23 PHP
用js统计用户下载网页所需时间的脚本
2008/10/15 Javascript
找出字符串中出现次数最多的字母和出现次数精简版
2012/11/07 Javascript
js中如何复制一个对象并获取其所有属性和属性对应的值
2013/10/24 Javascript
jquery插件lazyload.js延迟加载图片的使用方法
2014/02/19 Javascript
页面内容排序插件jSort使用方法
2015/10/10 Javascript
JS实现超简单的鼠标拖动效果
2015/11/02 Javascript
原生js实现移动端瀑布流式代码示例
2015/12/18 Javascript
前端框架Vue.js中Directive知识详解
2016/09/12 Javascript
老生常谈jquery中detach()和remove()的区别
2017/03/02 Javascript
关于使用js算总价的问题
2017/06/23 Javascript
python操作gmail实例
2015/01/14 Python
python如何实现远程控制电脑(结合微信)
2015/12/21 Python
Python中如何优雅的合并两个字典(dict)方法示例
2017/08/09 Python
Python异常对代码运行性能的影响实例解析
2018/02/08 Python
Python使用jsonpath-rw模块处理Json对象操作示例
2018/07/31 Python
python安装pywin32clipboard的操作方法
2019/01/24 Python
对Pytorch中nn.ModuleList 和 nn.Sequential详解
2019/08/18 Python
Python 实现黑客帝国中的字符雨的示例代码
2020/02/20 Python
浅谈对python中if、elif、else的误解
2020/08/20 Python
python使用matplotlib的savefig保存时图片保存不完整的问题
2021/01/08 Python
什么叫应用程序域?什么是托管代码?什么是强类型系统?什么是装箱和拆箱?什么是重载?CTS、CLS和CLR分别作何解释?
2012/05/23 面试题
高级人员简历的自我评价分享
2013/11/03 职场文书
商务英语专业求职信范文
2014/01/28 职场文书
奥巴马开学演讲稿
2014/05/15 职场文书
2014幼儿园教师师德师风演讲稿
2014/09/10 职场文书
十二生肖观后感
2015/06/12 职场文书
企业法律事务工作总结
2015/08/11 职场文书
90条交通安全宣传标语
2019/10/12 职场文书
Mysql存储过程、触发器、事件调度器使用入门指南
2022/01/22 MySQL