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 ...
随机推荐
- [转]http://lua-users.org/wiki/LpegTutorial
Simple Matching LPeg is a powerful notation for matching text data, which is more capable than Lua s ...
- Android 学习第3课,小例子
package temperature.convert; import java.util.Scanner; public class Converter { public static void m ...
- 搜狗输入法弹出搜狐新闻的解决办法(sohunews.exe)
狗输入法弹出搜狐新闻的解决办法(sohunews.exe) 1.找到搜狗输入法的安装目录(一般是C:\program files\sougou input\版本号\)2.右键点击sohunews.ex ...
- Map/Reduce 工作机制分析 --- 作业的执行流程
前言 从运行我们的 Map/Reduce 程序,到结果的提交,Hadoop 平台其实做了很多事情. 那么 Hadoop 平台到底做了什么事情,让 Map/Reduce 程序可以如此 "轻易& ...
- css中的字体及文本相关属性
css中的字体及文本相关属性 1.字体相关属性 字体主要可以设置color.font-family.font-size.font-size-adjust.font-stretch.font-style ...
- 打印心形---print 的基础使用
#!/bin/usr/env python#coding=utf-8'''用学习到的print语句,完成如下图形的打印工作打印心形'''print " *** *** "print ...
- Java加密算法 RSA
Java加密算法 RSA 2015-06-06 08:44 511人阅读 评论(0) 收藏 举报 分类: JAVA(57) 公钥加密也称为非对称加密.速度慢.加密和解密的钥匙不相同,某一个人持有私 ...
- linux -小记(2)问题:yum 安装报错"Another app is currently holding the yum lock; waiting for it to exit... ...: yum Memory : 26 M RSS (868 MB VSZ) Started: Wed Oct 26 22:48:24 2016 - 0"
yum 安装报错 "Another app is currently holding the yum lock; waiting for it to exit... The other ap ...
- SpringMVC学习系列(1) 之 初识SpringMVC
1.前言: 与SpringMVC的结识源于个人之前想做一个微信公众账号的管理平台玩玩,既然要做就需要考虑平台的选择问题.由于我的朋友只有一台运行了Linux系统的虚拟主机,且上面还运行有他自己的一些论 ...
- 【freemaker】之获取xml的值
测试代码 @Test public void test09() throws Exception{ root.put("doc", NodeModel.parse(new Inpu ...