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中的装饰器
Jul 31 Python
python字符串连接方法分析
Apr 12 Python
django基础之数据库操作方法(详解)
May 24 Python
python 简单备份文件脚本v1.0的实例
Nov 06 Python
Python cookbook(数据结构与算法)从序列中移除重复项且保持元素间顺序不变的方法
Mar 13 Python
python3.4.3下逐行读入txt文本并去重的方法
Apr 29 Python
Python 利用内置set函数对字符串和列表进行去重的方法
Jun 29 Python
selenium3+python3环境搭建教程图解
Dec 07 Python
Python类和对象的定义与实际应用案例分析
Dec 27 Python
PyQt4实时显示文本内容GUI的示例
Jun 14 Python
python pip安装包出现:Failed building wheel for xxx错误的解决
Dec 25 Python
总结Pyinstaller打包的高级用法
Jun 28 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
joomla jce editor 解决上传中文名文件失败问题
2013/06/09 PHP
phpnow php探针环境检测代码
2014/11/04 PHP
PHP实现加密的几种方式介绍
2015/02/22 PHP
PHP的邮件群发系统phplist配置方法详细总结
2016/03/30 PHP
php打包压缩文件之ZipArchive方法用法分析
2016/04/30 PHP
CentOS下搭建PHP环境与WordPress博客程序的全流程总结
2016/05/07 PHP
基于php实现的验证码小程序
2016/12/13 PHP
PHP中类型转换 ,常量,系统常量,魔术常量的详解
2017/10/26 PHP
PHP实现的mongoDB数据库操作类完整实例
2018/04/10 PHP
PHP使用file_get_contents发送http请求功能简单示例
2018/04/29 PHP
dreamweaver 安装Jquery智能提示
2011/04/02 Javascript
解析JavaScript中点号“.”的多义性
2013/12/02 Javascript
AngularJS中的过滤器filter用法完全解析
2016/04/22 Javascript
微信小程序教程之本地图片上传(leancloud)实例详解
2016/11/16 Javascript
ES5 ES6中Array对象去除重复项的方法总结
2017/04/27 Javascript
JavaScript动态绑定详解
2017/09/14 Javascript
JS实现预加载视频音频/视频获取截图(返回canvas截图)
2017/10/09 Javascript
js如何实现元素曝光上报
2019/08/07 Javascript
vue+element实现动态加载表单
2020/12/13 Vue.js
[09:40]DAC2018 4.5 SOLO赛 MidOne vs Miracle
2018/04/06 DOTA
[01:06:42]VP vs NewBee Supermajor 胜者组 BO3 第二场 6.5
2018/06/06 DOTA
python字典排序实例详解
2015/05/20 Python
pandas.DataFrame 根据条件新建列并赋值的方法
2018/04/08 Python
python调用staf自动化框架的方法
2018/12/26 Python
python循环定时中断执行某一段程序的实例
2019/06/29 Python
浅析python表达式4+0.5值的数据类型
2020/02/26 Python
Python importlib动态导入模块实现代码
2020/04/16 Python
浅谈keras使用中val_acc和acc值不同步的思考
2020/06/18 Python
python之随机数函数的实现示例
2020/12/30 Python
不打扫卫生检讨书
2014/02/12 职场文书
爸爸的花儿落了教学反思
2014/02/20 职场文书
给老师的感谢信
2015/01/20 职场文书
教师节校长致辞
2015/07/31 职场文书
初中化学教学反思
2016/02/22 职场文书
Python基础之tkinter图形化界面学习
2021/04/29 Python
浅谈pytorch中的dropout的概率p
2021/05/27 Python