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中用于返回绝对值的abs()方法
May 14 Python
Python函数式编程指南(二):从函数开始
Jun 24 Python
python如何通过protobuf实现rpc
Mar 06 Python
基于ID3决策树算法的实现(Python版)
May 31 Python
浅谈Python中带_的变量或函数命名
Dec 04 Python
python编写简易聊天室实现局域网内聊天功能
Jul 28 Python
python 寻找离散序列极值点的方法
Jul 10 Python
Selenium使用Chrome模拟手机浏览器方法解析
Apr 10 Python
Django 5种类型Session使用方法解析
Apr 29 Python
Python Json数据文件操作原理解析
May 09 Python
python中adb有什么功能
Jun 07 Python
scrapy中如何设置应用cookies的方法(3种)
Sep 22 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安全配置 如何配置使其更安全
2011/12/16 PHP
php中http_build_query 的一个问题
2012/03/25 PHP
php检查日期函数checkdate用法实例
2015/03/19 PHP
Aster vs KG BO3 第三场2.19
2021/03/10 DOTA
javascript Xml增删改查(IE下)操作实现代码
2009/01/30 Javascript
面向对象的Javascript之三(封装和信息隐藏)
2012/01/27 Javascript
Javascript面向对象编程
2012/03/18 Javascript
jquery打开直接跳到网页最下面、最低端实现代码
2013/04/22 Javascript
jQuery给多个不同元素添加class样式的方法
2015/03/26 Javascript
JavaScript ParseFloat()方法
2015/12/18 Javascript
BootStrap的Datepicker控件使用心得分享
2016/05/25 Javascript
深入浅析JavaScript中的scrollTop
2016/07/11 Javascript
vue-cli webpack 开发环境跨域详解
2017/05/18 Javascript
基于 Bootstrap Datetimepicker 联动
2017/08/03 Javascript
浅谈Angular2 模块懒加载的方法
2017/10/04 Javascript
使用element-ui table expand展开行实现手风琴效果
2019/03/15 Javascript
Vue2.x通用编辑组件的封装及应用详解
2019/05/28 Javascript
详解微信小程序开发(项目从零开始)
2019/06/06 Javascript
javascript随机变色实例代码
2019/10/15 Javascript
在微信小程序中使用mqtt服务的方法
2019/12/13 Javascript
javascript 设计模式之享元模式原理与应用详解
2020/04/08 Javascript
js 执行上下文和作用域的相关总结
2021/02/08 Javascript
Python使用正则表达式过滤或替换HTML标签的方法详解
2017/09/25 Python
Python操作MySQL数据库的三种方法总结
2018/01/30 Python
Python 机器学习库 NumPy入门教程
2018/04/19 Python
浅析python参数的知识点
2018/12/10 Python
python执行scp命令拷贝文件及文件夹到远程主机的目录方法
2019/07/08 Python
Stührling手表官方网站:男女高品质时尚手表的领先零售商
2021/01/07 全球购物
《猫》教学反思
2014/02/26 职场文书
我的生日感言
2015/08/03 职场文书
关于国庆节的广播稿
2015/08/19 职场文书
高中生物教学反思
2016/02/20 职场文书
《敬重卑微》读后感3篇
2019/11/26 职场文书
一文搞清楚MySQL count(*)、count(1)、count(col)区别
2022/03/03 MySQL
Redis之RedisTemplate配置方式(序列和反序列化)
2022/03/13 Redis
TV动画「神渣☆爱豆」公开第一弹主视觉图
2022/03/21 日漫