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解释器理解Python中的字节码
Apr 01 Python
python脚本内运行linux命令的方法
Jul 02 Python
Pycharm学习教程(7)虚拟机VM的配置教程
May 04 Python
Python实现获取命令行输出结果的方法
Jun 10 Python
python 输出上个月的月末日期实例
Apr 11 Python
基于数据归一化以及Python实现方式
Jul 11 Python
在python中使用with打开多个文件的方法
Jan 07 Python
python 判断矩阵中每行非零个数的方法
Jan 26 Python
在Pytorch中计算卷积方法的区别详解(conv2d的区别)
Jan 03 Python
opencv python图像梯度实例详解
Feb 04 Python
Python多线程thread及模块使用实例
Apr 28 Python
Python+腾讯云服务器实现每日自动健康打卡
Dec 06 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中用数组的方法设置cookies
2011/04/21 PHP
一个php生成16位随机数的代码(两种方法)
2014/09/16 PHP
thinkPHP中多维数组的遍历方法
2016/01/09 PHP
JQuery 实现的页面滚动时浮动窗口控件
2009/07/10 Javascript
jQuery UI 应用不同Theme的办法
2010/09/12 Javascript
判断ie的两种简单方法
2013/08/12 Javascript
HTML页面滚动时获取离页面顶部的距离2种实现方法
2013/09/05 Javascript
javascript中简单的进制转换代码实例
2013/10/26 Javascript
js获取input长度并根据页面宽度设置其大小及居中对齐
2014/08/22 Javascript
快速学习jQuery插件 Cookie插件使用方法
2015/12/01 Javascript
js实现内容显示并使用json传输数据
2016/03/16 Javascript
jQuery Validate 数组 全部验证问题
2017/01/12 Javascript
angularJS之$http:与服务器交互示例
2017/03/17 Javascript
Three.js利用性能插件stats实现性能监听的方法
2017/09/25 Javascript
Vue单页面应用保证F5强刷不清空数据的解决方案
2018/01/31 Javascript
vue 组件使用中的一些细节点
2018/04/25 Javascript
vue中如何实现pdf文件预览的方法
2018/07/12 Javascript
JavaScript对JSON数组简单排序操作示例
2019/01/31 Javascript
jquery更改元素属性attr()方法操作示例
2020/05/22 jQuery
python修改注册表终止360进程实例
2014/10/13 Python
python数据处理实战(必看篇)
2017/06/11 Python
python中的变量如何开辟内存
2018/06/26 Python
对Python定时任务的启动和停止方法详解
2019/02/19 Python
关于python多重赋值的小问题
2019/04/17 Python
对Python的交互模式和直接运行.py文件的区别详解
2019/06/29 Python
Python如何调用JS文件中的函数
2019/08/16 Python
基于python操作ES实例详解
2019/11/16 Python
python:HDF和CSV存储优劣对比分析
2020/06/08 Python
Python 如何实现数据库表结构同步
2020/09/29 Python
英国天然抗衰老护肤品品牌:Nakin Skin Care
2019/04/16 全球购物
2014年创卫实施方案
2014/02/18 职场文书
村主任个人对照检查材料
2014/10/01 职场文书
简单租房协议书(范本)
2014/10/13 职场文书
举起手来观后感
2015/06/09 职场文书
2017元旦、春节期间廉洁自律承诺书
2016/03/25 职场文书
微信小程序 根据不同用户切换不同TabBar
2022/04/21 Javascript