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 ...
随机推荐
- Mac下到Linux主机ssh免密码登录
最近忙得忘乎所以,写篇博客放松放松,RT,直接上命令好了 # Local ssh-keygen -t rsa scp ~/.ssh/id_rsa.pub username@server:~/.ssh/ ...
- bxSlider 在网页里添加幻灯片效果
幻灯片效果在网页上很常见,本文介绍用 bxSlider 轻松实现的方法. bxSlider是什么 bxSlider 是用 JQuery 和 CSS 实现网页中幻灯片效果的工具.可在 http://bx ...
- 项目Postmortem
设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 解决网站前端的数据处理以及获取问题,定义的很清楚,对于典型用户也比较清晰,因为主要只有一个用户,所以对于 ...
- HDfs命令
HDFS命令分为用户命令(dfs,fsck等),管理员命令(dfsadmn,namenode,datanode等) hdfs -ls -lsr 执行lsr 是递归显示 drwxr-xr-x -hado ...
- c# winform 关闭窗体时同时结束线程实现思路
Thread th = new Thread(Excute); th.IsBackground = true;这样就解决问题了. 这个属性的意思就是把线程设置为后台线程. 然后关闭进程的同时,线程也会 ...
- WorldChat.lua --世界聊天
print(">>Script: Wolrd Chating.") local SAY=" "--普通聊天 --联盟阵营 --部落阵营 local ...
- Android Service初始
一.Service概念 1.Service是一个应用程序组件 2.Service没有图像化界面 3.Service通常用来处理一些耗时比较长的操作 4.可以使用Service更新ContentProv ...
- Android-Cdma手机定位
对于Android CDMA手机获取当前位置,其实有更便利的办法,就是哄骗CdmaCellLocation.getBaseStationLatitude(),然则getBaseStationLatit ...
- [转]C++ DLL远程注入与卸载函数
代码是别处的 第一个函数是成功的,第二个函数运行发现会将目标程序挂死,也许是目标程序有保护机制 支持Unicode编码. //------------------------------------- ...
- Mysql对用户操作加审计功能——初级版
在某些应用里,需要知道谁对表进行了操作,进行了什么操作,所为责任的追朔.在MYSQL里,可以使用触发器实现. 1:创建测试表 mysql> create table A(a int);Query ...