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标准库inspect的具体使用方法
Dec 06 Python
教你用Python创建微信聊天机器人
Mar 31 Python
解决python3爬虫无法显示中文的问题
Apr 12 Python
python寻找list中最大值、最小值并返回其所在位置的方法
Jun 27 Python
浅谈Python中的bs4基础
Oct 21 Python
Python找出微信上删除你好友的人脚本写法
Nov 01 Python
用Q-learning算法实现自动走迷宫机器人的方法示例
Jun 03 Python
python抓取多种类型的页面方法实例
Nov 20 Python
解决TensorFlow模型恢复报错的问题
Feb 06 Python
Python判断三段线能否构成三角形的代码
Apr 12 Python
python中最小二乘法详细讲解
Feb 19 Python
Flask搭建一个API服务器的步骤
May 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
析构函数与php的垃圾回收机制详解
2013/10/28 PHP
PHP处理二进制数据的实现方法
2016/06/13 PHP
微信网页授权(OAuth2.0) PHP 源码简单实现
2016/08/29 PHP
PHP操作XML中XPath的应用示例
2019/07/04 PHP
CentOS7系统搭建LAMP及更新PHP版本操作详解
2020/03/26 PHP
定义select的边框颜色
2008/04/28 Javascript
jQuery的链式调用浅析
2010/12/03 Javascript
window.onresize 多次触发的解决方法
2013/11/08 Javascript
详解jquery uploadify 上传文件
2013/11/09 Javascript
js中AppendChild与insertBefore的用法详细解析
2013/12/16 Javascript
jQuery实现的经典滑动门效果
2015/09/22 Javascript
NodeJS与HTML5相结合实现拖拽多个文件上传到服务器的实现方法
2016/07/26 NodeJs
javaScript中封装的各种写法示例(推荐)
2017/07/03 Javascript
Angularjs中date过滤器失效的问题及解决方法
2018/07/06 Javascript
利用jquery和BootStrap实现动态滚动条效果
2018/12/03 jQuery
如何解决webpack-dev-server代理常切换问题
2019/01/09 Javascript
[54:47]Liquid vs VP Supermajor决赛 BO 第五场 6.10
2018/07/05 DOTA
Python中实现结构相似的函数调用方法
2015/03/10 Python
python结合API实现即时天气信息
2016/01/19 Python
Python删除Java源文件中全部注释的实现方法
2017/08/30 Python
详解Python map函数及Python map()函数的用法
2017/11/16 Python
Python实现的栈(Stack)
2018/01/26 Python
python的pandas工具包,保存.csv文件时不要表头的实例
2018/06/14 Python
Python实现统计英文文章词频的方法分析
2019/01/28 Python
在Python中通过threshold创建mask方式
2020/02/19 Python
Python TKinter如何自动关闭主窗口
2020/02/26 Python
Html5如何唤起百度地图App的方法
2019/01/27 HTML / CSS
语文教育专业推荐信范文
2013/11/25 职场文书
物业保安主管岗位职责
2013/12/25 职场文书
蛋糕店的商业计划书范文
2014/01/27 职场文书
80后职场人的职业生涯规划
2014/03/08 职场文书
腾讯广告词
2014/03/19 职场文书
班级课外活动总结
2014/07/09 职场文书
党员检讨书
2014/10/13 职场文书
趣味运动会口号
2015/12/24 职场文书
《风不能把阳光打败》读后感3篇
2020/01/06 职场文书