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 pdb调试方法分享
Jan 21 Python
Python中map,reduce,filter和sorted函数的使用方法
Aug 17 Python
Python实现快速排序和插入排序算法及自定义排序的示例
Feb 16 Python
Python简单连接MongoDB数据库的方法
Mar 15 Python
python 基础教程之Map使用方法
Jan 17 Python
tensorflow saver 保存和恢复指定 tensor的实例讲解
Jul 26 Python
Python print不能立即打印的解决方式
Feb 19 Python
Python3如何判断三角形的类型
Apr 12 Python
不到20行实现Python代码即可制作精美证件照
Apr 24 Python
一文详述 Python 中的 property 语法
Sep 01 Python
python实现逻辑回归的示例
Oct 09 Python
tensorflow中的梯度求解及梯度裁剪操作
May 26 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
第二节--PHP5 的对象模型
2006/11/16 PHP
需要发散思维学习PHP
2009/06/29 PHP
php中header跳转使用include包含解决参数丢失问题
2015/05/08 PHP
PHP查看当前变量类型的方法
2015/07/31 PHP
在WordPress中获取数据库字段内容和添加主题设置菜单
2016/01/11 PHP
php接口技术实例详解
2016/12/07 PHP
javascript xml为数据源的下拉框控件
2009/07/07 Javascript
js获取GridView中行数据的两种方法 分享
2013/07/13 Javascript
js hover 定时器(实例代码)
2013/11/12 Javascript
如何在node的express中使用socket.io
2014/12/15 Javascript
jQuery实现感应鼠标动画效果自动伸长的输入框实例
2015/02/24 Javascript
3种js实现string的substring方法
2015/11/09 Javascript
理解Javascript图片预加载
2016/02/23 Javascript
JavaScript探测CSS动画是否已经完成的方法
2016/08/30 Javascript
vue实现简单实时汇率计算功能
2017/01/15 Javascript
想用好React的你必须要知道的一些事情
2017/07/24 Javascript
vue项目刷新当前页面的三种方法
2018/12/04 Javascript
[07:54]DOTA2 MV《我的动力鞋》 ImbaTV 出品
2014/11/21 DOTA
Python3实现的腾讯微博自动发帖小工具
2013/11/11 Python
Python应用库大全总结
2018/05/30 Python
浅谈利用numpy对矩阵进行归一化处理的方法
2018/07/11 Python
tensorflow实现加载mnist数据集
2018/09/08 Python
python使用zip将list转为json的方法
2018/12/31 Python
CSS3实现类似翻书效果的过渡动画的示例代码
2019/09/06 HTML / CSS
CSS3 filter(滤镜)实现网页灰色或者黑色模式的示例代码
2021/02/24 HTML / CSS
视光学专业毕业生推荐信
2013/10/28 职场文书
清华大学自主招生自荐信
2014/01/29 职场文书
快餐店的创业计划书范文
2014/01/29 职场文书
餐饮企业总经理岗位职责范文
2014/02/18 职场文书
促销活动总结模板
2014/07/01 职场文书
电焊工岗位工作职责
2014/07/09 职场文书
暑期培训班策划方案
2014/08/26 职场文书
党员个人对照检查材料范文
2014/09/24 职场文书
2014年团委工作总结
2014/11/13 职场文书
学校社团活动总结
2015/05/07 职场文书
学校德育工作总结2015
2015/05/11 职场文书