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代码实例
Feb 04 Python
在Python的框架中为MySQL实现restful接口的教程
Apr 08 Python
Python实现Linux的find命令实例分享
Jun 04 Python
influx+grafana自定义python采集数据和一些坑的总结
Sep 17 Python
Python3实现的简单三级菜单功能示例
Mar 12 Python
浅谈Python中的异常和JSON读写数据的实现
Feb 27 Python
Python 自由定制表格的实现示例
Mar 20 Python
基于python实现简单C/S模式代码实例
Sep 14 Python
Restful_framework视图组件代码实例解析
Nov 17 Python
python可视化分析的实现(matplotlib、seaborn、ggplot2)
Feb 03 Python
python爬虫--selenium模块
Mar 31 Python
手把手教你怎么用Python实现zip文件密码的破解
May 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
关于更改Zend Studio/Eclipse代码风格主题的介绍
2013/06/23 PHP
PHP根据传来的16进制颜色代码自动改变背景颜色
2014/06/13 PHP
PHP经典面试题集锦
2015/03/19 PHP
jQuery 使用手册(一)
2009/09/23 Javascript
JavaScript 拾漏补遗
2009/12/27 Javascript
JavaScript的漂亮的代码片段
2013/06/05 Javascript
javascript中的正则表达式使用详解
2015/08/30 Javascript
基于Javascript实现文件实时加载进度的方法
2016/10/12 Javascript
JavaScript实现时钟滴答声效果
2017/01/29 Javascript
vue子父组件通信的实现代码
2017/07/09 Javascript
vue.js与后台数据交互的实例讲解
2018/08/08 Javascript
vue+iview使用树形控件的具体使用
2020/11/02 Javascript
如何封装Vue Element的table表格组件
2021/02/06 Vue.js
python实现class对象转换成json/字典的方法
2016/03/11 Python
Python脚本实现12306火车票查询系统
2016/09/30 Python
python实现感知器算法详解
2017/12/19 Python
python print 按逗号或空格分隔的方法
2018/05/02 Python
Python实现微信消息防撤回功能的实例代码
2019/04/29 Python
python中使用ctypes调用so传参设置遇到的问题及解决方法
2019/06/19 Python
Pandas实现一列数据分隔为两列
2020/05/18 Python
django为Form生成的label标签添加class方式
2020/05/20 Python
Keras 数据增强ImageDataGenerator多输入多输出实例
2020/07/03 Python
使用css3匹配手机屏幕横竖状态
2014/01/27 HTML / CSS
设计师家具购买和委托在线市场:Viyet
2016/11/16 全球购物
护理专业推荐信
2013/11/07 职场文书
计算机专业学生的自我评价
2013/12/15 职场文书
秋季运动会稿件
2014/01/30 职场文书
中职生自荐信范文
2014/06/15 职场文书
敬老院标语
2014/06/27 职场文书
艺术设计专业毕业生推荐信
2014/07/08 职场文书
2015初中团委工作总结
2015/07/28 职场文书
《普罗米修斯》教学反思
2016/02/22 职场文书
redis客户端实现高可用读写分离的方式详解
2021/07/04 Redis
java后台调用接口及处理跨域问题的解决
2022/03/24 Java/Android
SpringBoot深入分析讲解监听器模式下
2022/07/15 Java/Android
MySQL下载安装配置详细教程 附下载资源
2022/09/23 MySQL