"); //-->
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技術交流群:943406067。
*博客內容為網友個人發布,僅代表博主個人觀點,如有侵權請聯系工作人員刪除。
相關推薦
電子設備安全試驗的基本原則及其試驗項目和試驗要求
CC2520 社區視頻
高頻高壓振蕩電路(直流電壓變換器)
并行接口的沒落與串行接口的崛起
DAC8564 社區視頻
串聯諧振電路
磁擺振蕩電路
電子器件
場效應晶體管低壓振蕩電路
轉發郵件, 三星arm芯片有售
互補管自激多諧振蕩器電路
連接孿生: 完善物聯網和數字孿生戰略的必備要素
Marvell 推出定制 HBM 計算架構:XPU 同 HBM 間 IO 接口更小更強
意法半導體車規八通道柵極驅動引入專利技術,降低電機驅動設計的物料成本
CC2431.CC2430及Zigbee應用的片上系統(SOC)解決方案
抄板過程大曝光
怎么理解這個條例
《嵌入式系統編程源代碼解析》--嵌入式初學推薦!
在這里道一聲:大家新年好!
xMEMS“氣冷式主動散熱芯片”榮獲CES 2025創新獎
電子工程師指南
Marvell 推出定制 HBM 計算架構:XPU 同 HBM 間 I/O 接口更小更強
電子線路(提高版·模擬電路與脈沖數字電路)
秒懂濾波
關于計數信號量和消息隊列的一個問題
CC2591 社區視頻
采用創新型 C29 內核的 MCU 如何 提升高壓系統的實時性能
電子線路PSPICE分析與設計
CC2530 片上系統視頻
瑞薩推出全新Type-C端口控制器和升降壓電池充電器,以及基于此兩款產品的高性能USB PD EPR解決方案