python生成随机验证码(中文验证码)示例


Posted in Python onApril 03, 2014
# -*- 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)
Python 相关文章推荐
python利用elaphe制作二维条形码实现代码
May 25 Python
Python中实现对list做减法操作介绍
Jan 09 Python
Python3多线程操作简单示例
May 22 Python
Python跳出多重循环的方法示例
Jul 03 Python
Numpy数组array和矩阵matrix转换方法
Aug 05 Python
解决Python计算矩阵乘向量,矩阵乘实数的一些小错误
Aug 26 Python
Python操作excel的方法总结(xlrd、xlwt、openpyxl)
Sep 02 Python
Django中间件拦截未登录url实例详解
Sep 03 Python
通过python连接Linux命令行代码实例
Feb 18 Python
matplotlib 画动态图以及plt.ion()和plt.ioff()的使用详解
Jan 05 Python
PyTorch 如何检查模型梯度是否可导
Jun 05 Python
Python Pandas pandas.read_sql_query函数实例用法分析
Jun 21 Python
python读取html中指定元素生成excle文件示例
Apr 03 #Python
python实现zencart产品数据导入到magento(python导入数据)
Apr 03 #Python
python模拟登陆阿里妈妈生成商品推广链接
Apr 03 #Python
python多线程抓取天涯帖子内容示例
Apr 03 #Python
python局域网ip扫描示例分享
Apr 03 #Python
python实现数通设备tftp备份配置文件示例
Apr 02 #Python
python实现巡检系统(solaris)示例
Apr 02 #Python
You might like
php无限极分类递归排序实现方法
2014/11/11 PHP
php遍历CSV类实例
2015/04/14 PHP
PHP redis实现超迷你全文检索
2017/03/04 PHP
另一个javascript小测验(代码集合)
2011/07/27 Javascript
jQuery之折叠面板的深入解析
2013/06/19 Javascript
javascript实现TreeView 无刷新展开的实例代码
2013/07/13 Javascript
JavaScript调试技巧之console.log()详解
2014/03/19 Javascript
node.js中的http.response.getHeader方法使用说明
2014/12/14 Javascript
jQuery中next()方法用法实例
2015/01/07 Javascript
微信内置浏览器私有接口WeixinJSBridge介绍
2015/05/25 Javascript
JS实现无限级网页折叠菜单(类似树形菜单)效果代码
2015/09/17 Javascript
JQuery学习总结【一】
2016/12/01 Javascript
实例详解JSON取值(key是中文或者数字)方式
2017/08/24 Javascript
深入理解Vue官方文档梳理之全局API
2017/11/22 Javascript
bootstrap treeview 树形菜单带复选框及级联选择功能
2018/06/08 Javascript
微信小程序实现天气预报功能
2018/07/18 Javascript
vue里面使用mui的弹出日期选择插件实例
2018/09/16 Javascript
vue中当图片地址无效的时候,显示默认图片的方法
2018/09/18 Javascript
详解微信小程序框架wepy踩坑记录(与vue对比)
2019/03/12 Javascript
python套接字流重定向实例汇总
2016/03/03 Python
Python 实现12306登录功能实例代码
2018/02/09 Python
使用python Fabric动态修改远程机器hosts的方法
2018/10/26 Python
python 多线程中子线程和主线程相互通信方法
2018/11/09 Python
pygame游戏之旅 添加游戏界面按键图形
2018/11/20 Python
解决Jupyter notebook中.py与.ipynb文件的import问题
2020/04/21 Python
使用Keras预训练好的模型进行目标类别预测详解
2020/06/27 Python
python如何遍历指定路径下所有文件(按按照时间区间检索)
2020/09/14 Python
python ssh 执行shell命令的示例
2020/09/29 Python
Python+Appium实现自动化清理微信僵尸好友的方法
2021/02/04 Python
联想韩国官网:Lenovo Korea
2018/05/10 全球购物
英语专业毕业生自我鉴定
2013/11/09 职场文书
经理秘书岗位职责
2013/11/14 职场文书
党员干部对十八届四中全会的期盼
2014/10/17 职场文书
中小学生安全教育观后感
2015/06/17 职场文书
2016学习医德医风心得体会
2016/01/25 职场文书
PostgreSQL事务回卷实战案例详析
2022/03/25 PostgreSQL