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中的标识运算符
May 14 Python
Python安装第三方库的3种方法
Jun 21 Python
在Python的Django框架中调用方法和处理无效变量
Jul 15 Python
Python下的常用下载安装工具pip的安装方法
Nov 13 Python
Python字典,函数,全局变量代码解析
Dec 18 Python
Python cookbook(数据结构与算法)通过公共键对字典列表排序算法示例
Mar 15 Python
django 通过ajax完成邮箱用户注册、激活账号的方法
Apr 17 Python
安装完Python包然后找不到模块的解决步骤
Feb 13 Python
Django admin管理工具TabularInline类用法详解
May 14 Python
tensorflow实现从.ckpt文件中读取任意变量
May 26 Python
Python利用capstone实现反汇编
Apr 06 Python
详解Python中*args和**kwargs的使用
Apr 07 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
模仿OSO的论坛(五)
2006/10/09 PHP
php木马攻击防御之道
2008/03/24 PHP
php中检查文件或目录是否存在的代码小结
2012/10/22 PHP
PHP的MVC模式实现原理分析(一相简单的MVC框架范例)
2014/04/29 PHP
PHP实现事件机制的方法
2015/07/10 PHP
PHP实现QQ、微信和支付宝三合一收款码实例代码
2018/02/19 PHP
JS 文字符串转换unicode编码函数
2009/05/30 Javascript
JS去掉第一个字符和最后一个字符的实现代码
2014/02/20 Javascript
JS创建类和对象的两种不同方式
2014/08/08 Javascript
jQuery对指定元素中指定字符串进行替换的方法
2015/03/17 Javascript
jQuery实现鼠标滚动图片延迟加载效果附源码下载
2016/06/28 Javascript
jQuery简单实现页面元素置顶时悬浮效果示例
2016/08/01 Javascript
js基础之事件捕获与冒泡原理
2019/10/09 Javascript
Node.js API详解之 readline模块用法详解
2020/05/22 Javascript
vue v-model的用法解析
2020/10/19 Javascript
微信 用脚本查看是否被微信好友删除
2016/10/28 Python
Python中的取模运算方法
2018/11/10 Python
Python WEB应用部署的实现方法
2019/01/02 Python
python 计算积分图和haar特征的实例代码
2019/11/20 Python
Pytorch在dataloader类中设置shuffle的随机数种子方式
2020/01/14 Python
html5通过canvas实现刮刮卡效果示例分享
2014/01/27 HTML / CSS
美国护肤咨询及美容产品电商:Askderm
2017/02/24 全球购物
美国在线宠物商店:Chewy
2019/01/12 全球购物
网站域名和主机:Domain.com
2019/04/01 全球购物
eBay爱尔兰站:eBay.ie
2019/08/09 全球购物
到底Java是如何传递参数的?是by value或by reference?
2012/07/13 面试题
法学个人求职信范文
2014/01/27 职场文书
放飞梦想演讲稿
2014/05/05 职场文书
教师职位说明书
2014/07/29 职场文书
党员争先创优承诺书
2015/01/20 职场文书
大学学生个人总结
2015/02/15 职场文书
写给导师的自荐信
2015/03/06 职场文书
2015年幼儿园个人工作总结
2015/04/25 职场文书
离婚起诉书范文2015
2015/05/19 职场文书
歌剧魅影观后感
2015/06/05 职场文书
用JS创建一个录屏功能
2021/11/11 Javascript