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 MySQLdb模块连接操作mysql数据库实例
Apr 08 Python
python根据日期返回星期几的方法
Jul 06 Python
Python复制文件操作实例详解
Nov 10 Python
python实现简单爬虫功能的示例
Oct 24 Python
Python3.6通过自带的urllib通过get或post方法请求url的实例
May 10 Python
基于Python List的赋值方法
Jun 23 Python
详解如何用python实现一个简单下载器的服务端和客户端
Oct 28 Python
python SVD压缩图像的实现代码
Nov 05 Python
基于python实现图片转字符画代码实例
Sep 04 Python
python 字符串格式化的示例
Sep 21 Python
Python3+Flask安装使用教程详解
Feb 16 Python
Python音乐爬虫完美绕过反爬
Aug 30 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与SQL注入攻击防范小技巧
2011/09/16 PHP
mod_php、FastCGI、PHP-FPM等PHP运行方式对比
2015/07/02 PHP
php5.3/5.4/5.5/5.6/7常见新增特性汇总整理
2020/02/27 PHP
ExtJS 下拉多选框lovcombo
2010/05/19 Javascript
JavaScript中继承的一些示例方法与属性参考
2010/08/07 Javascript
Js sort排序使用方法
2011/10/17 Javascript
中文路径导致unitpngfix.js不正常的解决方法
2013/06/26 Javascript
JQuery的自定义事件代码,触发,绑定简单实例
2013/08/01 Javascript
在AngularJS应用中实现一些动画效果的代码
2015/06/18 Javascript
JavaScript实现复制或剪切内容到剪贴板功能的方法
2016/05/23 Javascript
Redux 和 Mobx的选择问题:让你不再困惑!
2017/09/18 Javascript
详解Webpack loader 之 file-loader
2018/11/07 Javascript
从零开始在NPM上发布一个Vue组件的方法步骤
2018/12/20 Javascript
原生js实现点击按钮复制内容到剪切板
2020/11/19 Javascript
python网页请求urllib2模块简单封装代码
2014/02/07 Python
Python中__init__.py文件的作用详解
2016/09/18 Python
python在每个字符后添加空格的实例
2018/05/07 Python
python 把列表转化为字符串的方法
2018/10/23 Python
python字典一键多值实例代码分享
2019/06/14 Python
python切片的步进、添加、连接简单操作示例
2019/07/11 Python
Python人工智能之路 之PyAudio 实现录音 自动化交互实现问答
2019/08/13 Python
PyCharm下载和安装详细步骤
2019/12/17 Python
Python hashlib加密模块常用方法解析
2019/12/18 Python
Python如何访问字符串中的值
2020/02/09 Python
解决python3.x安装numpy成功但import出错的问题
2020/11/17 Python
Pycharm 解决自动格式化冲突的设置操作
2021/01/15 Python
各大浏览器 CSS3 和 HTML5 兼容速查表 图文
2010/04/01 HTML / CSS
css3 边框、背景、文本效果的实现代码
2018/03/21 HTML / CSS
匡威西班牙官网:Converse西班牙
2019/10/01 全球购物
工厂门卫岗位职责
2013/11/25 职场文书
员工考核管理制度
2014/02/02 职场文书
数学高效课堂实施方案
2014/03/29 职场文书
党的生日演讲稿
2014/09/10 职场文书
2015年九一八事变纪念日演讲稿
2015/03/19 职场文书
2015年小班保育员工作总结
2015/05/27 职场文书
vue实现锚点定位功能
2021/06/29 Vue.js