python生成中文验证码,带旋转,带干扰噪音线段
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 4 15:57:46 2015 @author: keithguofan
""" import random
from PIL import Image,ImageDraw,ImageFont
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')
# return str.encode('gb2312').decode('gb2312')
return codecs.decode(str,'hex_codec').decode('gb2312') class ImageChar():
def __init__(self,fontColor=(0,0,0),
size=(100,40),
# fontPath='C:/Windows/Fonts/wqy.ttc',
fontPath='C:/Windows/Fonts/simsun.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)
# print(pos)
# print(txt)
#print(self.font)
#print(fill)
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) if __name__ == '__main__':
ic=ImageChar(fontColor=(100,211,90))
ic.randChinese(4)
ic.save("5.jpeg")
最近在研究验证码识别,中文的验证码不太好识别,上面的代码我参考了一些博客,亲测通过。我的python环境是python3.4
生成的图片大概是这个样子:

代码很简单。
核心思想是:
生成随机十六进制数字,然后将其转换为汉字,通过ImageDraw的方法将其打印到图片中,对字做一些随机旋转,然后随机加入随机颜色的线段。
如有不懂的可以联系我哦。
python生成中文验证码,带旋转,带干扰噪音线段的更多相关文章
- Python 生成随机验证码
Python生成随机验证码 Python生成随机验证码,需要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 fro ...
- Python生成随机验证码
Python生成随机验证码,需要使用PIL模块. 安装: pip3 install pillow 基本使用 1.创建图片 from PIL import Image img = Image.new(m ...
- PHP生成中文验证码并检测对错实例
PHP生成中文验证码并检测对错实例,中文验证码的例子还是比较少的,今天给大家分享一下,支持自定义中文.字体.背景色等 生成验证码,注意font字体路径要对,否则显示图片不存在 session_star ...
- python生成随机图形验证码
使用python生成随机图片验证码,需要使用pillow模块 1.安装pillow模块 pip install pillow 2.pillow模块的基本使用 1.创建图片 from PIL impor ...
- Python生成随机验证码,大乐透号码
实例笔记之生成随机号码 扩展知识 - yield(生成器) 随机生成验证码 示例代码: import random # 导入标准模块中的random if __name__ == '__main__' ...
- 使用Python生成基础验证码教程
pillow是Python平台事实上的图像处理标准库.PIL功能非常强大,但API却非常简单易用. 所以我们使用它在环境里做图像的处理. 第一步 下载pillow #运行命令 pip install ...
- python 生成图形验证码
文章链接:https://mp.weixin.qq.com/s/LYUBRNallHcjnhJb1R3ZBg 日常在网站使用过程中经常遇到图形验证,今天准备自己做个图形验证码,这算是个简单的功能,也适 ...
- 利用python生成图形验证码
validCode.py import random from io import BytesIO from PIL import Image, ImageDraw, ImageFont def ge ...
- Python - 生成随机验证码的3种实现方式
生成6位随机验证码的3种实现方式如下: 1. 简单粗暴型:所有数字和字母都放入字符串: 2. 利用ascii编码的规律,遍历获取字符串和数字的字符串格式: 3. 引用string库. 方法1代码: i ...
随机推荐
- crontab 移动日志-超越昨天的自己系列(12)
linux上定时执行某些脚本是管理服务器的时候比较常用的场景,比如定时检查进程是否存在,定时启动或关闭进程,定时检查日志删除日志等. 当我打开google百度crontab时长篇大论的一大堆,详细解释 ...
- python数据结构与算法——二叉树结构与遍历方法
先序遍历,中序遍历,后序遍历 ,区别在于三条核心语句的位置 层序遍历 采用队列的遍历操作第一次访问根,在访问根的左孩子,接着访问根的有孩子,然后下一层 自左向右一一访问同层的结点 # 先序遍历 # ...
- IE6 IE7 不支持 JSON
最近发现ie6.7不支持json,解决方法:引入json2.js <script type="text/javascript" src="json2.js& ...
- 腾讯优测-优社区干货精选 | android开发在路上:少去踩坑,多走捷径(下)
文/腾讯公司 陈江峰 优测小优有话说: android开发的坑自然是不少,不想掉坑快来优测优社区~ 6.Android APP开发中其它需要提醒的问题 android4.4在UI线程无法进行网络操作. ...
- distributed 以及 distributional 语义表达的区别
昨天在审核资讯翻译的时候遇到这两个单词,文章里讲的也不清楚,感觉翻译的也不到位.今天就单独拿出来比较一下吧.尽管这两个单词相似,它们的具体实现方法还是有很大区别的. Distributional wo ...
- hiho一下122周 后缀数组三·重复旋律
后缀数组三·重复旋律3 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为长度为 N 的数构成的数列.小Hi ...
- Bean的定义及作用域的注解实现
1. Classpath扫描与组件管理 从Spring3.0开始,Spring JavaConfig项目提供了很多特性,包括使用java而不是XML定义bean. 比如@configuration, ...
- Android学习七:new Date使用
1.例子 学习时间函数,并实现了简单的多个按钮监听同一个事件的方法 2.代码 代码很简单,也很清晰 package com.example.datetime; import java.text.Sim ...
- ubuntu 编译安装 srilm
Ubuntu 64bit系统下SRILM的配置 依赖软件包(先进行): 1.c/c++ compiler:编译器gcc 3.4.3及以上版本,我的是gcc 4.4 2.GNU make:构建和管理工程 ...
- 在VBA中调用winsock控件
如果系统没有Winsock控件的话,可以下载下面的控件MSWINSCK.OCX,然后将该文件复制到C:\Windows\System32目录下. 在VBE窗口中,从菜单"工具"-& ...