Python培训之PIL模块处理图片及中文验证码实例

2018-03-16 10:02:19 989浏览

PIL是PythonImagingLibrary的简称,PIL是一个Python处理图片的库,提供了一系列模块和方法,比如:裁切,平移,旋转,改变尺寸等等。已经是Python平台事实上的图像处理标准库了。PIL功能非常强大,但API却非常简单易用。

有如下几个模块:Image模块、ImageChops模块、ImageCrackCode模块、ImageDraw模块、ImageEnhance模块、ImageFile模块、ImageFileIO模块、ImageFilter模块、ImageFont模块、ImageGrab模块、ImageOps模块、ImagePath模块、ImageSequence模块、ImageStat模块、ImageTk模块、ImageWin模块、PSDraw模块。



在PIL库中,任何一个图像都是用Image对象来表示的,所以要加载一张图片,最简单的形式如下:

fromPILimportImage

image=Image.open("1.jpeg")

在PIL库中,最常用的模块有Image,ImageDraw,ImageEnhance,ImageFile等。

接下来学习PIL中一些常见的操作:

1、改变图片大小

使用resize方法可以方便的对图片进行缩放

fromPILimportImage

image=Image.open("1.jpeg")

new_img=image.resize((256,256),Image.BILINEAR)

new_img.save("2.jpeg")

2.旋转图片

rotate方法可以进行旋转操作

fromPILimportImage

image=Image.open("1.jpeg")

new_img=image.rotate(45)

new_img.save("2.jpeg")

3.直线绘制

PIL库中的ImageDraw模块提供了图形绘制的基本功能,可以绘制直线,弧线,椭圆,矩形等等。

fromPILimportImage,ImageDraw

image=Image.open("2.jpeg")

draw=ImageDraw.Draw(image)

width,height=image.size

draw.line(((0,0),(width-1,height-1)),fill=255)

draw.line(((0,height-1),(width-1,0)),fill=255)

image.save("2.jpeg")

4.绘制圆

fromPILimportImage,ImageDraw

image=Image.open("2.jpeg")

draw=ImageDraw.Draw(image)

width,height=image.size

draw.arc((0,0,width-1,height-1),0,360,fill=255)

image.save("2.jpeg")

5.中文验证码实例

现在我们来研究利用Python如何生成中文验证码。其实这个问题简单,我们都知道验证码一般是用来防止网络机器

人采用无限次数的登录尝试破解密码,那么我们需要对这张图片随机生成中文字符来验证,一般来说会把图片的字表

现得很模糊,这样有效地防止机器人识别。所以我们除了要随机生成汉字,还要加入一些干扰线条等等。

#-*-coding:utf-8-*-

fromPILimportImage,ImageDraw,ImageFont

importrandom

importmath,string

classRandomChar():

@staticmethod

defUnicode():

val=random.randint(0x4E00,0x9FBF)

returnunichr(val)

@staticmethod

defGB2312():

head=random.randint(0xB0,0xCF)

body=random.randint(0xA,0xF)

tail=random.randint(0,0xF)

val=(head<<8)|(body<<4)|tail

str="%x"%val

returnstr.decode('hex').decode('gb2312')

classImageChar:

def__init__(self,fontColor=(0,0,0),

size=(100,40),

fontPath='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)

defrotate(self):

self.image.rotate(random.randint(0,30),expand=0)

defdrawText(self,pos,txt,fill):

draw=ImageDraw.Draw(self.image)

draw.text(pos,txt,font=self.font,fill=fill)

defrandRGB(self):

return(random.randint(0,255),

random.randint(0,255),

random.randint(0,255))

defrandPoint(self):

(width,height)=self.size

return(random.randint(0,width),random.randint(0,height))

defrandLine(self,num):

draw=ImageDraw.Draw(self.image)

foriinrange(0,num):

draw.line([self.randPoint(),self.randPoint()],self.randRGB())

defrandChinese(self,num):

gap=5

start=0

foriinrange(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)

defsave(self,path):

self.image.save(path)

ic=ImageChar(fontColor=(100,211,90))

ic.randChinese(4)

ic.save("1.jpeg")

以上就是关于扣丁学堂Python培训之PIL模块处理图片及中文验证码实例,最后想要了解更多关于Python发展前景趋势,请关注扣丁学堂python培训官网、微信等平台,扣丁学堂IT职业在线学习教育平台为您提供最新的Python视频教程系统,通过千锋扣丁学堂金牌讲师在线录制的Python视频教程课程,让你快速掌握Python从入门到精通开发实战技能。扣丁学堂Python开发工程师技术交流群:279521237。


扣丁学堂微信公众号



关注微信公众号获取更多学习资料



查看更多关于"Python开发资讯"的相关文章>>

标签: Python视频教程 Python基础教程 Python爬虫 Python培训 Python开发工程师

热门专区

暂无热门资讯

课程推荐

微信
微博
15311698296

全国免费咨询热线

邮箱:codingke@1000phone.com

官方群:148715490

北京千锋互联科技有限公司版权所有   北京市海淀区宝盛北里西区28号中关村智诚科创大厦4层
京ICP备12003911号-6   Copyright © 2013 - 2019

京公网安备 11010802030908号