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字符串替换的2种方法
Nov 30 Python
使用Mixin设计模式进行Python编程的方法讲解
Jun 21 Python
Python实现在线暴力破解邮箱账号密码功能示例【测试可用】
Sep 06 Python
Python中optparser库用法实例详解
Jan 26 Python
Python实现确认字符串是否包含指定字符串的实例
May 02 Python
Python3使用pandas模块读写excel操作示例
Jul 03 Python
python3 中文乱码与默认编码格式设定方法
Oct 31 Python
python-itchat 获取微信群用户信息的实例
Feb 21 Python
python使用Plotly绘图工具绘制柱状图
Apr 01 Python
python递归调用中的坑:打印有值, 返回却None
Mar 16 Python
Pytorch 使用CNN图像分类的实现
Jun 16 Python
Python pandas之求和运算和非空值个数统计
Aug 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
PHP获取当前日期所在星期(月份)的开始日期与结束日期(实现代码)
2013/06/18 PHP
php开发微信支付获取用户地址
2015/10/04 PHP
使用PHP如何实现高效安全的ftp服务器(一)
2015/12/20 PHP
php实现购物车功能(下)
2016/01/05 PHP
Laravel 5.5 的自定义验证对象/类示例代码详解
2017/08/29 PHP
Extjs学习笔记之八 继承和事件基础
2010/01/08 Javascript
JQuery入门—编写一个简单的JQuery应用案例
2013/01/03 Javascript
模拟电子签章盖章效果的jQuery插件源码
2013/06/24 Javascript
JavaScript fontcolor方法入门实例(按照指定的颜色来显示字符串)
2014/10/17 Javascript
AngularJS ng-bind 指令简单实现
2016/07/30 Javascript
AngularJS 作用域详解及示例代码
2016/08/17 Javascript
关于Javascript中defer和async的区别总结
2016/09/20 Javascript
Javascript动画效果(3)
2016/10/11 Javascript
Bootstrap基本组件学习笔记之input输入框组(9)
2016/12/07 Javascript
JavaScript内存泄漏的处理方式
2017/11/20 Javascript
bootstrap treeview 扩展addNode方法动态添加子节点的方法
2017/11/21 Javascript
[05:46]DOTA2英雄梦之声_第18期_陈
2014/06/20 DOTA
[01:07:34]DOTA2-DPC中国联赛定级赛 RNG vs Aster BO3第二场 1月9日
2021/03/11 DOTA
python从ftp下载数据保存实例
2013/11/20 Python
Python 实现自动导入缺失的库
2019/10/29 Python
基于Pycharm加载多个项目过程图解
2020/01/19 Python
Python逐行读取文件内容的方法总结
2020/02/14 Python
python应用Axes3D绘图(批量梯度下降算法)
2020/03/25 Python
python 使用raw socket进行TCP SYN扫描实例
2020/05/05 Python
python3 通过 pybind11 使用Eigen加速代码的步骤详解
2020/12/07 Python
美国正版电视节目和电影在线观看:Hulu
2018/05/24 全球购物
为女性购买传统的印度服装和婚纱:Kalkifashion
2019/07/22 全球购物
如何写你的创业计划书
2014/01/07 职场文书
市政管理求职信范文
2014/05/07 职场文书
监督检查工作方案
2014/05/28 职场文书
5s标语大全
2014/06/23 职场文书
小学生国庆65周年演讲稿范文(2篇)
2014/09/21 职场文书
师德师风个人整改措施
2014/10/27 职场文书
个人年度总结报告
2015/03/09 职场文书
公司放假通知范文
2015/04/14 职场文书
心得体会该怎么写呢?
2019/06/27 职场文书