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实现DNS正向查询、反向查询的例子
Apr 25 Python
Python的ORM框架中SQLAlchemy库的查询操作的教程
Apr 25 Python
Python遍历指定文件及文件夹的方法
May 09 Python
python中常用的九种预处理方法分享
Sep 11 Python
详解python的webrtc库实现语音端点检测
May 31 Python
关于Django外键赋值问题详解
Aug 13 Python
python机器学习理论与实战(一)K近邻法
Jan 28 Python
TensorFlow实现AutoEncoder自编码器
Mar 09 Python
python实现小程序推送页面收录脚本
Apr 20 Python
Python3自动生成MySQL数据字典的markdown文本的实现
May 07 Python
如何用Python绘制3D柱形图
Sep 16 Python
微软开源最强Python自动化神器Playwright(不用写一行代码)
Jan 05 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中uploaded_files函数使用方法详解
2011/03/09 PHP
PHP fprintf()函数用法讲解
2019/02/16 PHP
符合标准的js表单提交的代码
2007/09/13 Javascript
js 禁用浏览器的后退功能的简单方法
2008/12/10 Javascript
淘宝搜索框效果实现分析
2011/03/05 Javascript
jquery增加时编辑jqGrid(实例代码)
2013/11/08 Javascript
jqplot通过ajax动态画折线图的方法及思路
2013/12/08 Javascript
ListBox实现上移,下移,左移,右移的简单实例
2014/02/13 Javascript
JavaScipt中栈的实现方法
2016/02/17 Javascript
JS操作JSON方法总结(推荐)
2016/06/14 Javascript
点击页面任何位置隐藏div的实现方法
2016/09/05 Javascript
JS运动特效之同时运动实现方法分析
2018/01/24 Javascript
Vue中v-for的数据分组实例
2018/03/07 Javascript
深入理解Node module模块
2018/03/26 Javascript
用Node提供静态文件服务的方法
2018/07/06 Javascript
使用Sonarqube扫描Javascript代码的示例
2018/12/26 Javascript
解决Vue+Electron下Vuex的Dispatch没有效果问题
2019/05/20 Javascript
基于Element封装一个表格组件tableList的使用方法
2020/06/29 Javascript
python在控制台输出进度条的方法
2015/06/20 Python
深入浅析Python中join 和 split详解(推荐)
2016/06/30 Python
总结用Pdb库调试Python的方式及常用的命令
2016/08/18 Python
用生成器来改写直接返回列表的函数方法
2017/05/25 Python
Python3中的列表,元组,字典,字符串相关知识小结
2017/11/10 Python
python+selenium打印当前页面的titl和url方法
2018/06/22 Python
解决Python Matplotlib绘图数据点位置错乱问题
2020/05/16 Python
Tensorflow之MNIST CNN实现并保存、加载模型
2020/06/17 Python
Pycharm 解决自动格式化冲突的设置操作
2021/01/15 Python
上学迟到的检讨书
2014/01/11 职场文书
大学四年个人自我小结
2014/03/05 职场文书
追悼会主持词
2014/03/20 职场文书
拾金不昧锦旗标语
2014/06/27 职场文书
公务员爱岗敬业演讲稿
2014/08/26 职场文书
会计试用期自我评价怎么写
2014/09/18 职场文书
python中对列表的删除和添加方法详解
2022/02/24 Python
利用Python实现翻译HTML中的文本字符串
2022/06/21 Python
CSS 鼠标选中文字后改变背景色的实现代码
2023/05/21 HTML / CSS