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判断IP地址合法性的方法实例
Mar 13 Python
python实现带验证码网站的自动登陆实现代码
Jan 12 Python
Python中Collections模块的Counter容器类使用教程
May 31 Python
教你用Python脚本快速为iOS10生成图标和截屏
Sep 22 Python
python实现守护进程、守护线程、守护非守护并行
May 05 Python
python实现随机漫步算法
Aug 27 Python
详解PyCharm安装MicroPython插件的教程
Jun 24 Python
Python爬虫 urllib2的使用方法详解
Sep 23 Python
django中瀑布流写法实例代码
Oct 14 Python
Python  word实现读取及导出代码解析
Jul 09 Python
Django返回HTML文件的实现方法
Sep 17 Python
Python爬虫实现selenium处理iframe作用域问题
Jan 27 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四种基本排序算法示例
2015/04/09 PHP
Laravel (Lumen) 解决JWT-Auth刷新token的问题
2019/10/24 PHP
限制复选框的最大可选数
2006/07/01 Javascript
深入理解JavaScript系列(13) This? Yes,this!
2012/01/18 Javascript
封装html的select标签的js操作实例
2013/07/02 Javascript
javascript获取四位数字或者字母的随机数
2015/01/09 Javascript
Ztree新增角色和编辑角色回显问题的解决
2016/10/25 Javascript
js实现导航栏中英文切换效果
2017/01/16 Javascript
AngularJs表单校验功能实例代码
2017/02/09 Javascript
如何写好你的JavaScript【推荐】
2017/03/02 Javascript
JS 判断某变量是否为某数组中的一个值的3种方法(总结)
2017/07/10 Javascript
VUE2 前端实现 静态二级省市联动选择select的示例
2018/02/09 Javascript
vue页面离开后执行函数的实例
2018/03/13 Javascript
微信小程序入门之广告条实现方法示例
2018/12/05 Javascript
Nodejs监听日志文件的变化的过程解析
2019/08/04 NodeJs
解决nuxt页面中mounted、created、watch执行两遍的问题
2020/11/05 Javascript
vue打开其他项目页面并传入数据详解
2020/11/25 Vue.js
Python实现爬取知乎神回复简单爬虫代码分享
2015/01/04 Python
Python随机生成数据后插入到PostgreSQL
2016/07/28 Python
浅谈python函数之作用域(python3.5)
2017/10/27 Python
python使用标准库根据进程名如何获取进程的pid详解
2017/10/31 Python
通过pycharm使用git的步骤(图文详解)
2019/06/13 Python
python 解决flask uwsgi 获取不到全局变量的问题
2019/12/22 Python
pytorch 图像预处理之减去均值,除以方差的实例
2020/01/02 Python
使用python的pyplot绘制函数实例
2020/02/13 Python
Python控制台输出时刷新当前行内容而不是输出新行的实现
2020/02/21 Python
Python制作数据预测集成工具(值得收藏)
2020/08/21 Python
美国学校用品、教室和教学商店:Discount School Supply
2018/04/04 全球购物
现代生活方式的家具和装饰:Dot & Bo
2018/12/26 全球购物
Scotch Porter官方网站:男士美容产品
2020/08/31 全球购物
介绍一下linux的文件系统
2012/03/20 面试题
求职简历中个人的自我评价
2013/12/01 职场文书
求职自我推荐信
2014/06/25 职场文书
党员四风问题对照检查材料
2014/09/27 职场文书
上课玩手机的检讨书
2014/10/01 职场文书
Tomcat项目启动失败的原因和解决办法
2022/04/20 Servers