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多线程ctrl+c退出问题解决方案
Oct 23 Python
Django的分页器实例(paginator)
Dec 01 Python
用python写扫雷游戏实例代码分享
May 27 Python
django反向解析URL和URL命名空间的方法
Jun 05 Python
python tkinter窗口最大化的实现
Jul 15 Python
讲解Python3中NumPy数组寻找特定元素下标的两种方法
Aug 04 Python
python滑块验证码的破解实现
Nov 10 Python
python的json中方法及jsonpath模块用法分析
Dec 06 Python
Python的对象传递与Copy函数使用详解
Dec 26 Python
Python接口自动化判断元素原理解析
Feb 24 Python
python+playwright微软自动化工具的使用
Feb 02 Python
python 实现Requests发送带cookies的请求
Feb 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 如何获取二维数组中某个key的集合
2014/06/03 PHP
Php连接及读取和写入mysql数据库的常用代码
2014/08/11 PHP
JQuery之focus函数使用介绍
2013/08/20 Javascript
js重写alert控件(适合学习js的新手朋友)
2014/08/24 Javascript
HTML,CSS,JavaScript速查表推荐
2014/12/02 Javascript
angular ng-repeat数组中的数组实例
2017/02/18 Javascript
详解JS中的立即执行函数
2017/02/24 Javascript
nodeJS模块简单用法示例
2018/04/21 NodeJs
AngularJS模态框模板ngDialog的使用详解
2018/05/11 Javascript
JavaScript中filter的用法实例分析
2019/02/27 Javascript
Python Web服务器Tornado使用小结
2014/05/06 Python
Python爬取读者并制作成PDF
2015/03/10 Python
Python中time模块与datetime模块在使用中的不同之处
2015/11/24 Python
linux环境下的python安装过程图解(含setuptools)
2017/11/22 Python
pip安装Python库时遇到的问题及解决方法
2017/11/23 Python
在Mac上删除自己安装的Python方法
2018/10/29 Python
Python使用pyautocad+openpyxl处理cad文件示例
2019/07/11 Python
python自动生成model文件过程详解
2019/11/02 Python
python 读取二进制 显示图片案例
2020/04/24 Python
Python matplotlib读取excel数据并用for循环画多个子图subplot操作
2020/07/14 Python
selenium如何定位span元素的实现
2021/01/13 Python
HTML5超文本标记语言的实现方法
2020/09/24 HTML / CSS
韩国三星旗下的一家超市连锁店:Home Plus
2016/07/30 全球购物
SmartBuyGlasses荷兰:购买太阳镜和眼镜
2020/03/16 全球购物
英国著名的美容护肤和护发产品购物网站:Lookfantastic
2020/11/23 全球购物
企业管理专业个人求职信范文
2013/09/24 职场文书
客户经理岗位职责
2013/12/08 职场文书
酒店采购员岗位职责
2014/03/14 职场文书
请假条怎么写
2014/04/10 职场文书
医院深入开展党的群众路线教育实践活动实施方案
2014/08/27 职场文书
财务审计整改报告
2014/11/06 职场文书
服务行业标语口号
2015/12/26 职场文书
预备党员入党思想汇报(范文)
2019/08/14 职场文书
Html5页面播放M4a音频文件
2021/03/30 HTML / CSS
MySQL系列之开篇 MySQL关系型数据库基础概念
2021/07/02 MySQL
使用ICOM IC-R9500接收机同时测评十台收音机中波接收性能
2022/05/10 无线电