# -*- 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生成中文验证码,带旋转,带干扰噪音线段的更多相关文章

  1. Python 生成随机验证码

    Python生成随机验证码  Python生成随机验证码,需要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 fro ...

  2. Python生成随机验证码

    Python生成随机验证码,需要使用PIL模块. 安装: pip3 install pillow 基本使用 1.创建图片 from PIL import Image img = Image.new(m ...

  3. PHP生成中文验证码并检测对错实例

    PHP生成中文验证码并检测对错实例,中文验证码的例子还是比较少的,今天给大家分享一下,支持自定义中文.字体.背景色等 生成验证码,注意font字体路径要对,否则显示图片不存在 session_star ...

  4. python生成随机图形验证码

    使用python生成随机图片验证码,需要使用pillow模块 1.安装pillow模块 pip install pillow 2.pillow模块的基本使用 1.创建图片 from PIL impor ...

  5. Python生成随机验证码,大乐透号码

    实例笔记之生成随机号码 扩展知识 - yield(生成器) 随机生成验证码 示例代码: import random # 导入标准模块中的random if __name__ == '__main__' ...

  6. 使用Python生成基础验证码教程

    pillow是Python平台事实上的图像处理标准库.PIL功能非常强大,但API却非常简单易用. 所以我们使用它在环境里做图像的处理. 第一步 下载pillow #运行命令 pip install ...

  7. python 生成图形验证码

    文章链接:https://mp.weixin.qq.com/s/LYUBRNallHcjnhJb1R3ZBg 日常在网站使用过程中经常遇到图形验证,今天准备自己做个图形验证码,这算是个简单的功能,也适 ...

  8. 利用python生成图形验证码

    validCode.py import random from io import BytesIO from PIL import Image, ImageDraw, ImageFont def ge ...

  9. Python - 生成随机验证码的3种实现方式

    生成6位随机验证码的3种实现方式如下: 1. 简单粗暴型:所有数字和字母都放入字符串: 2. 利用ascii编码的规律,遍历获取字符串和数字的字符串格式: 3. 引用string库. 方法1代码: i ...

随机推荐

  1. 简单介绍Javascript匿名函数和面向对象编程

    忙里偷闲,简单介绍一下Javascript中匿名函数和闭包函数以及面向对象编程.首先简单介绍一下Javascript中的密名函数. 在Javascript中函数有以下3中定义方式: 1.最常用的定义方 ...

  2. input屏蔽历史记录

    设置input 的扩展属性 autocomplete为off即可 <input type="text" autocomplete="off" />

  3. 通过SSH远程使用ipython notebook

    本文讲述如何在本地用浏览器运行远程服务器上的iPython notebook服务. 在远程机器上,启动IPython notebooks服务: remote_user@remote_host$ ipy ...

  4. poj3181 Dollar Dayz ——完全背包

    link:http://poj.org/problem?id=3181 本来很常规的一道完全背包,比较有意思的一点是,结果会超int,更有意思的解决方法是,不用高精度,用两个整型的拼接起来就行了.OR ...

  5. 配置vim环境

    <1> 一般不建议更改/etc/vimrc这个文件,因为此文件更改对所有用户生效, 故一般只更改当前用户 即更改文件 vim ~/.vimrc (.表示隐藏文件) <2> 该文 ...

  6. Asm.Def点大兵

    syzoj上的题,收货很多,orz天天学长 原题: Asm.Def奉命组建一支m人的特种作战小队前往圣迭戈.他有n名候选人,可以在其中任意挑选.由于小队中每个人都有独特的作用,所以次序不同的两种选法被 ...

  7. 转载部长一篇大作:常用排序算法之JavaScript实现

    转载部长一篇大作:常用排序算法之JavaScript实现 注:本文是转载实验室同门王部长的大作,找实习找工作在即,本文颇有用处!原文出处:http://www.cnblogs.com/ywang172 ...

  8. [Freemarker] - 使用struts的component调用freemarker的ftl模板方法

    struts中的component标签,可以用来调用freemarker的ftl模板文件,使用component标签传参可以这样写: 使用property方式写法: <s:component t ...

  9. 《C#编程风格》还记得多少

    开始实习之后,才发现自己是多么地菜.还有好多东西还要去学习. 公司很好,还可以帮你买书.有一天随口问了一下上司D,代码规范上面有什么要求.然后D在Amazon上面找到了这本书<C#编程风格(Th ...

  10. MyBatis jdbcType常用类型

    MyBatis jdbcType常用类型 jdbcType与javaType对应关系 javaType jdbctype CHAR String VARCHAR String LONGVARCHAR ...