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 迭代器和iter()函数详解及实例
Mar 21 Python
Python中enumerate()函数编写更Pythonic的循环
Mar 06 Python
对Python 窗体(tkinter)文本编辑器(Text)详解
Oct 11 Python
Python运维开发之psutil库的使用详解
Oct 18 Python
Python将列表数据写入文件(txt, csv,excel)
Apr 03 Python
基于Python2、Python3中reload()的不同用法介绍
Aug 12 Python
pytorch多GPU并行运算的实现
Sep 27 Python
Python实现二叉树的最小深度的两种方法
Sep 30 Python
TensorFLow 变量命名空间实例
Feb 11 Python
获取python运行输出的数据并解析存为dataFrame实例
Jul 07 Python
Python OpenCV超详细讲解基本功能
Apr 02 Python
Python自动化工具之实现Excel转Markdown表格
Apr 08 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设计模式 Prototype (原型模式)代码
2011/06/26 PHP
PHP下的Oracle客户端扩展(OCI8)安装教程
2014/09/10 PHP
PHP使用array_fill定义多维数组的方法
2015/03/18 PHP
symfony2.4的twig中date用法分析
2016/03/18 PHP
thinkphp3.2同时连接两个数据库的简单方法
2019/08/13 PHP
Javascript String对象扩展HTML编码和解码的方法
2009/06/02 Javascript
jQuery的实现原理的模拟代码 -1 核心部分
2010/08/01 Javascript
jQuery仿Excel表格编辑功能的实现代码
2013/05/01 Javascript
jQuery选择器源码解读(八):addCombinator函数
2015/03/31 Javascript
js实现简单鼠标跟随效果的方法
2015/04/10 Javascript
JavaScript中使用指数方法Math.exp()的简介
2015/06/15 Javascript
jquery利用拖拽方式在图片上添加热链接
2015/11/24 Javascript
深入解析桶排序算法及Node.js上JavaScript的代码实现
2016/07/06 Javascript
JS正则表达式修饰符中multiline(/m)用法分析
2016/12/27 Javascript
基于node.js实现微信支付退款功能
2017/12/19 Javascript
原生js实现移动端触摸轮播的示例代码
2017/12/22 Javascript
vue如何将v-for中的表格导出来
2018/05/07 Javascript
怎样使你的 JavaScript 代码简单易读(推荐)
2019/04/16 Javascript
vue框架制作购物车小球动画效果实例代码
2019/09/26 Javascript
JavaScript 面向对象程序设计详解【类的创建、实例对象、构造函数、原型等】
2020/05/12 Javascript
JavaScript图像放大镜效果实现方法详解
2020/06/28 Javascript
python实现倒计时的示例
2014/02/14 Python
python实现解数独程序代码
2017/04/12 Python
python使用mysql的两种使用方式
2018/03/07 Python
Python 25行代码实现的RSA算法详解
2018/04/10 Python
django mysql数据库及图片上传接口详解
2019/07/18 Python
flask 框架操作MySQL数据库简单示例
2020/02/02 Python
AUC计算方法与Python实现代码
2020/02/28 Python
python向企业微信发送文字和图片消息的示例
2020/09/28 Python
如何用python写个模板引擎
2021/01/14 Python
销售经理工作职责
2014/02/03 职场文书
电工实训报告总结
2014/11/05 职场文书
秋菊打官司观后感
2015/06/03 职场文书
师德师风培训感言
2015/08/03 职场文书
员工考勤管理制度
2015/08/06 职场文书
小学语文教学反思范文
2016/03/03 职场文书