当前位置: 首页 > 图灵资讯 > 行业资讯> 手把手教你用Python PIL模块随机生成中文验证码

手把手教你用Python PIL模块随机生成中文验证码

发布时间:2025-09-24 11:02:49

PIL是Python Imaging 简称Library,PIL是Python处理图片的库,提供切割、平移、旋转、改变尺寸等一系列模块和方法。它已经是Python平台实际上的图像处理标准库。PIL功能非常强大,但API非常简单易用。

PIL有以下模块:Image模块、ImageChops模块、ImageCrackCode模块、ImageDraw模块、Imagenhance模块、ImageFile模块、ImageFileIO模块、ImageFilter模块、ImageFilter模块、ImageGrab模块、ImageGrab模块、ImagePath模块、ImageSequence模块、ImageStat模块、ImageWin模块、PSDraw模块。

在PIL库中,任何图像都是由Image对象表示的,因此加载图片的最简单形式如下:

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

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

PIL下载地址如下:http://www.pythonware.com/products/pil/

接下来,学习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中reduce函数和lambda表达式的学习

Python中reduce函数和lambda表达式的学习

2025-09-25
Python小白必学的面向对象

Python小白必学的面向对象

2025-09-25
一个例子解释python装饰器

一个例子解释python装饰器

2025-09-25
深入理解Python的set和dict

深入理解Python的set和dict

2025-09-25
Python中正则表达式的巧妙使用

Python中正则表达式的巧妙使用

2025-09-25
5分钟搞定Python中函数的参数

5分钟搞定Python中函数的参数

2025-09-25