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编写网页爬虫脚本并实现APScheduler调度
Jul 28 Python
详解Python中的Cookie模块使用
Jul 06 Python
详解duck typing鸭子类型程序设计与Python的实现示例
Jun 03 Python
对python 多线程中的守护线程与join的用法详解
Feb 18 Python
python调用动态链接库的基本过程详解
Jun 19 Python
python实现超市商品销售管理系统
Nov 22 Python
django ListView的使用 ListView中获取url中的参数值方式
Mar 27 Python
Django调用百度AI接口实现人脸注册登录代码实例
Apr 23 Python
django 将自带的数据库sqlite3改成mysql实例
Jul 09 Python
python 如何利用argparse解析命令行参数
Sep 11 Python
python matplotlib工具栏源码探析三之添加、删除自定义工具项的案例详解
Feb 25 Python
python matplotlib工具栏源码探析二之添加、删除内置工具项的案例
Feb 25 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 批量更新网页内容实现代码
2010/01/05 PHP
php使用cookie实现记住登录状态
2015/04/27 PHP
Laravel框架实现定时发布任务的方法
2018/08/16 PHP
laravel 如何实现引入自己的函数或类库
2019/10/15 PHP
js实现的网站首页随机公告随机公告
2007/03/14 Javascript
ExtJS[Desktop]实现图标换行示例代码
2013/11/17 Javascript
nw.js实现类似微信的聊天软件
2015/03/16 Javascript
javascript实现给定半径求出圆的面积
2015/06/26 Javascript
JavaScript操作select元素和option的实例代码
2016/01/29 Javascript
微信小程序 扎金花简单实例
2017/02/21 Javascript
jQuery插件HighCharts绘制2D金字塔图效果示例【附demo源码下载】
2017/03/09 Javascript
JavaScript闭包原理与用法实例分析
2018/08/10 Javascript
详解vue移动端项目的适配(以mint-ui为例)
2018/08/17 Javascript
js使用文件流下载csv文件的实现方法
2019/07/15 Javascript
nuxt+axios实现打包后动态修改请求地址的方法
2020/04/22 Javascript
[01:11:15]VGJ.S vs Secret 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
python遍历文件夹并删除特定格式文件的示例
2014/03/05 Python
玩转python selenium鼠标键盘操作(ActionChains)
2020/04/12 Python
matplotlib实现热成像图colorbar和极坐标图的方法
2018/12/13 Python
对python判断是否回文数的实例详解
2019/02/08 Python
Django models.py应用实现过程详解
2019/07/29 Python
用 Python 制作地球仪的方法
2020/04/24 Python
Python drop方法删除列之inplace参数实例
2020/06/27 Python
Python如何进行时间处理
2020/08/06 Python
python使用建议与技巧分享(二)
2020/08/17 Python
北美最大的手工艺品零售商之一:Michaels Stores
2019/02/27 全球购物
CHARLES & KEITH台湾官网:新加坡时尚品牌
2019/07/30 全球购物
印度第一网上礼品店:IGP.com
2020/02/06 全球购物
数组越界问题
2015/10/21 面试题
酒店人事专员岗位职责
2013/12/19 职场文书
企业职业病防治方案
2014/05/29 职场文书
施工安全保证书
2015/05/09 职场文书
2015年个人实习工作总结
2015/05/28 职场文书
详解如何修改nginx的默认端口
2021/03/31 Servers
完美处理python与anaconda环境变量的冲突问题
2021/04/07 Python
世界无敌的ICOM IC-R9500宽频接收机
2022/03/25 无线电