溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python中PIL庫有什么用

發(fā)布時間:2021-08-26 11:10:47 來源:億速云 閱讀:186 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)Python中PIL庫有什么用,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

首先,給出Image模塊中的一個簡單的例子。例子實現(xiàn)的功能是:讀取圖片,并進(jìn)行45°旋轉(zhuǎn),然后進(jìn)行可視化。

# -*- coding:utf-8 -*-
# Image模塊開篇例子
from PIL import Image
im = Image.open('test.bmp') # 讀取圖片
im.rotate(45).show() # 將圖片旋轉(zhuǎn),并用系統(tǒng)自帶的圖片工具顯示圖片

創(chuàng)建縮略圖

# -*- coding:utf-8 -*-
# PIL中創(chuàng)建縮略圖(create thumbnails)
from PIL import Image
import glob,os

size = 128,128

for infile in glob.glob("*.jpg"): # glob的作用是文件搜索,返回的是一個列表
  file,ext = os.path.splitext(infile) # 將文件的文件名和拓展名分開,用于之后的保存重命名
  im = Image.open(infile)
  im.thumbnail(size,Image.ANTIALIAS) # 等比例縮放
  im.save(file+".thumbnail","JPEG")
  #im.show() # 顯示縮略圖
  #print im.size,im.mode

縮略圖不能直接雙擊打開,而可以使用PIL.image的open讀取,然后使用show()方法進(jìn)行顯示。

圖像處理

PIL.image.alpha_composite(im1,im2)

PIL.image.blend(im1,im2,alpha)

PIL.Image.composite(im1,im2,mask)

這三個方法都屬于圖片的合成或者融合。都要求im1和im2的mode和size要一致,alpha代表圖片占比的意思,而mask是mode可以為”1”,”L”或者”RGBA”的size和im1、im2一致的。

# coding:utf-8 -*-
from PIL import Image
# 圖片合成

# PIL的alpha_composite(im1,im2) 圖像通道融合
# im2要和im1的size和mode一致,且要求格式為RGBA
im1 = Image.open("test.png")
im2 = Image.open("test2.png")

newim1 = Image.alpha_composite(im1,im2) # 將im2合成到im1中,如果其一是透明的,
# 才能看到結(jié)果,不然最終結(jié)果只會顯示出im2
newim1.show()
#print(im1.mode)

# -----------------------------------------
# image.blend(im1,im2,alpha)
# alpha為透明度
newim2 = Image.blend(im1,im2,0.5)
newim2.show()

# -----------------------------------------
mask = Image.open("mask.png")
newim3 = Image.composite(im2,im1,mask)
newim3.show()

PIL.image.eval(image,*args)

程序:

# -*- coding:utf-8 -*-
from PIL import Image
im = Image.open("test.png")

imnew = Image.eval(im,lambda i:i*2) # 將原圖片的像素點,都乘2,返回的是一個Image對象
#print imnew.mode
imnew.show()
im.show()

創(chuàng)建圖像

(1) PIL.image.new(mode,size,color=0)

使用模式和大小,創(chuàng)建一個新的圖像。其中,mode可以是”L”,”RGB”,”RGBA”;而size則是一個tuple(元組),color應(yīng)該和mode相對應(yīng)。

下面例子,分別創(chuàng)建”L”、”RGB”和”RGBA”的圖片。

# -*- coding:utf-8 -*-
from PIL import Image
# 創(chuàng)建圖像
# 創(chuàng)建一個灰度圖像

newL = Image.new("L",(28,28),255)
newL.show()

# 創(chuàng)建一個RGb圖像
newrgb = Image.new("RGB",(28,28),(20,200,45))
newrgb.show()

newrgba = Image.new("RGBA",(28,28),(20,200,45,255))
newrgba.show()
print "The frist image:",newL.size,newL.mode
print "The second image:",newrgb.size,newrgb.mode
print "The third image:",newrgba.size,newrgba.mode

(2)以其他形式創(chuàng)建圖像

a. 以數(shù)組的形式創(chuàng)建圖像,PIL.image.fromarray(obj,mode=None)

obj - 圖像的數(shù)組,類型可以是numpy.array()

mode - 如果不給出,會自動判斷

本人覺得這個功能還是挺實用的,可以將一個數(shù)組(具體一點就是像素數(shù)組)轉(zhuǎn)換為圖像,從圖像的本質(zhì)去處理圖像。

下面一段程序,就是用fromarray()函數(shù)實現(xiàn)圖像的灰度化(使用了兩種方法)。

# -*- coding:utf-8 -*-
from PIL import Image
import numpy as np
a = Image.open("fromimg.png")
a.show()
b = a.resize((28,28))
datab = list(b.getdata())
#print type(datab)
obj1 = []
obj2 = []
for i in range(len(datab)):
  obj1.append([sum(datab[i])/3]) # 灰度化方法1:RGB三個分量的均值
  obj2.append([0.3*datab[i][0]+0.59*datab[i][1]+0.11*datab[i][2]])
  #灰度化方法2:根據(jù)亮度與RGB三個分量的對應(yīng)關(guān)系:Y=0.3*R+0.59*G+0.11*B

obj1 = np.array(obj1).reshape((28,28))
obj2 = np.array(obj2).reshape((28,28))
print obj1
print obj2

arrayimg1 = Image.fromarray(obj1)
arrayimg2 = Image.fromarray(obj2)
arrayimg1.show()
arrayimg2.show()

顯然,兩種方法都能成功灰度化。

還有:

PIL.Image.frombytes(mode,size,data,decoder_name='raw',*args)

PIL.Image.fromstring(*args,**kw)

PIL.Image.frombuffer(mode,size,data,decoder_name='raw',*args)

感覺不常用,沒有仔細(xì)研究。

Image模塊下的Image類

下面的Image是一個圖像對象,而不是模塊!

(1) Image.convert(mode=None,matrix=None,dither=None,palette=0,color=256)

該方法,同樣可以實現(xiàn)上面的灰度化處理。

# -*- coding:utf-8 -*-
from PIL import Image
img = Image.open("test.png")
# 灰度化:將RGB/RGBA -> L
img = img.convert("L")
img.show()

(2) Image.copy()

將讀取的圖片復(fù)制一份。

# -*- coding:utf-8 -*-
from PIL import Image
img = Image.open("test.png")
# 灰度化:將RGB/RGBA -> L
img = img.convert("L")
#img.show()

# ------ copy()----------
img1 = img.copy()
img1.show()

將灰度化的圖片復(fù)制一份,因此該程序的運(yùn)行結(jié)果和之前的一致。

(3) Image.filter(filter)

該函數(shù)是用于圖像濾波的,PIL中自帶了很多的濾波器,就是括號中的filter的參數(shù)。filter應(yīng)該是一個ImaageFilter模塊下的對象。這里把ImageFilter模塊講了。其實,該模塊就是提供濾波器。自帶的濾波器有:

使用中值濾波:

# -*- coding:utf-8 -*-
from PIL import Image
from PIL import ImageFilter

# BLUR - 模糊處理
# CONTOUR - 輪廓處理
# DETAIL - 增強(qiáng)
# EDGE_ENHANCE - 將圖像的邊緣描繪得更清楚
# EDGE_ENHANCE_NORE - 程度比EDGE_ENHANCE更強(qiáng)
# EMBOSS - 產(chǎn)生浮雕效果
# SMOOTH - 效果與EDGE_ENHANCE相反,將輪廓柔和
# SMOOTH_MORE - 更柔和
# SHARPEN - 效果有點像DETAIL
testimg = Image.open("filter1.png")
testimg.show()
filterimg = testimg.filter(ImageFilter.MedianFilter)
filterimg.show()

(4) 使用各種方法/函數(shù)獲取圖片的基本信息

Image.getbands()

Image.geebbox()

Image.getcolors(maxcolor=256)

Image.getdata(band=None)(一般和list()結(jié)合使用)

Image.getextrema()

Image.getpixel((x,y))

Image.histogram(mask=None,extrema=None)

# -*- coding:utf-8 -*-
from PIL import Image
img1 = Image.open("test.png")
img1.show()

# getbands() - 顯示該圖像的所有通道,返回一個tuple
bands = img1.getbands()
print bands

# getbbox() - 返回一個像素坐標(biāo),4個元素的tuple
bboxs = img1.getbbox()
print bboxs

# getcolors() - 返回像素信息,是一個含有元素的列表[(該種像素的數(shù)量,(該種像素)),(...),...]
colors = img1.getcolors()
print colors

# getdata() - 返回圖片所有的像素值,要使用list()才能顯示出具體數(shù)值
#data = list(img1.getdata())
#print data

# getextrema() - 獲取圖像中每個通道的像素最小和最大值,是一個tuple類型
extremas = img1.getextrema()
print extremas

# getpixel() - 獲取該坐標(biāo)
pixels = img1.getpixel((87,180))
print pixels

# histogram() - 返回圖片的像素直方圖
print(img1.histogram())

運(yùn)行結(jié)果:

('R', 'G', 'B', 'A')
(0, 0, 338, 238)
[(73463, (255, 255, 255, 255)), (32, (252, 249, 252, 255)), (1, (255, 189, 143, 255)), (12, (255, 199, 160, 255)), (22, (247, 239, 247, 255)), (3, (255, 242, 246, 255)), (9, (238, 221, 238, 255)), (9, (235, 215, 235, 255)), (5, (232, 209, 232, 255)), (1, (255, 228, 209, 255)), (2, (255, 210, 225, 255)), (1, (255, 202, 201, 255)), (3, (255, 158, 92, 255)), (22, (218, 181, 218, 255)), (1, (217, 181, 218, 255)), (2, (255, 232, 217, 255)), (16, (255, 195, 153, 255)), (22, (212, 169, 212, 255)), (3, (211, 169, 212, 255)), (1, (204, 153, 204, 255)), (1, (255, 229, 238, 255)), (53, (255, 131, 46, 255)), (9, (255, 203, 167, 255)), (1, (255, 157, 90, 255)), (3, (186, 119, 187, 255)), (2, (255, 217, 229, 255)), (6, (183, 113, 184, 255)), (1, (255, 212, 227, 255)), (14, (214, 175, 215, 255)), (2, (255, 182, 131, 255)), (12, (166, 79, 167, 255)), (2, (255, 180, 127, 255)), (4309, (255, 127, 39, 255)), (737, (163, 73, 164, 255)), (4, (255, 252, 253, 255)), (3, (255, 232, 216, 255)), (9, (255, 250, 233, 255)), (1, (255, 245, 248, 255)), (34, (255, 239, 228, 255)), (3, (255, 142, 64, 255)), (1, (255, 162, 98, 255)), (19, (255, 247, 241, 255)), (7, (255, 223, 201, 255)), (2, (255, 133, 49, 255)), (16, (255, 221, 232, 255)), (58, (255, 235, 221, 255)), (1, (255, 225, 204, 255)), (2, (255, 219, 194, 255)), (21, (255, 175, 120, 255)), (6, (255, 182, 206, 255)), (37, (255, 243, 235, 255)), (3, (255, 179, 127, 255)), (6, (255, 207, 223, 255)), (3, (255, 232, 240, 255)), (1, (255, 134, 51, 255)), (2, (255, 222, 233, 255)), (2, (255, 218, 192, 255)), (1, (255, 186, 186, 255)), (1, (255, 163, 99, 255)), (1, (255, 207, 173, 255)), (8, (255, 151, 80, 255)), (1, (255, 184, 201, 255)), (19, (255, 211, 180, 255)), (1, (255, 143, 65, 255)), (9, (255, 233, 158, 255)), (18, (255, 215, 187, 255)), (1, (255, 185, 136, 255)), (7, (255, 227, 237, 255)), (22, (255, 163, 100, 255)), (1, (255, 221, 198, 255)), (5, (255, 184, 208, 255)), (10, (255, 195, 215, 255)), (5, (255, 239, 182, 255)), (1, (255, 197, 157, 255)), (1, (255, 154, 85, 255)), (1, (255, 136, 55, 255)), (8, (255, 240, 190, 255)), (14, (255, 216, 229, 255)), (3, (255, 179, 204, 255)), (1, (255, 143, 67, 255)), (1, (255, 196, 155, 255)), (19, (255, 249, 227, 255)), (2, (255, 211, 181, 255)), (10, (255, 230, 142, 255)), (4, (255, 187, 140, 255)), (195, (255, 201, 14, 255)), (2, (255, 129, 42, 255)), (1, (255, 131, 47, 255)), (12, (255, 231, 214, 255)), (1, (255, 181, 151, 255)), (8, (249, 244, 249, 255)), (13, (246, 238, 246, 255)), (44, (244, 234, 244, 255)), (1, (243, 232, 244, 255)), (7, (240, 226, 241, 255)), (25, (255, 167, 107, 255)), (24, (255, 215, 229, 255)), (22, (230, 206, 230, 255)), (6, (229, 204, 229, 255)), (3, (255, 130, 45, 255)), (11, (227, 200, 228, 255)), (4, (226, 198, 226, 255)), (3, (255, 127, 40, 255)), (5, (223, 192, 223, 255)), (9, (220, 186, 221, 255)), (172, (255, 174, 201, 255)), (16, (255, 231, 239, 255)), (1, (255, 171, 113, 255)), (33, (209, 164, 209, 255)), (1, (255, 192, 213, 255)), (6, (255, 247, 250, 255)), (2, (255, 136, 54, 255)), (9, (255, 253, 247, 255)), (1, (255, 171, 114, 255)), (2, (255, 147, 73, 255)), (5, (255, 181, 130, 255)), (7, (189, 124, 190, 255)), (1, (255, 199, 161, 255)), (13, (255, 183, 134, 255)), (3, (255, 152, 82, 255)), (2, (255, 156, 88, 255)), (32, (255, 143, 66, 255)), (5, (178, 102, 178, 255)), (6, (175, 96, 176, 255)), (8, (255, 129, 43, 255)), (4, (172, 90, 173, 255)), (1, (255, 168, 109, 255)), (1, (255, 153, 83, 255)), (1, (255, 174, 118, 255)), (1, (255, 172, 115, 255)), (1, (255, 148, 75, 255)), (8, (255, 244, 248, 255)), (1, (255, 130, 43, 255)), (5, (255, 205, 222, 255)), (1, (255, 210, 177, 255)), (1, (255, 170, 110, 255)), (1, (255, 157, 89, 255)), (1, (255, 197, 134, 255)), (13, (255, 155, 86, 255)), (3, (255, 137, 56, 255)), (2, (255, 138, 57, 255)), (11, (255, 227, 208, 255)), (1, (255, 190, 145, 255)), (2, (255, 155, 87, 255)), (1, (169, 84, 170, 255)), (4, (255, 202, 220, 255)), (6, (255, 139, 59, 255)), (1, (255, 128, 42, 255)), (1, (255, 158, 91, 255)), (1, (255, 198, 158, 255)), (5, (255, 130, 44, 255)), (1, (255, 202, 165, 255)), (1, (255, 187, 154, 255)), (1, (255, 132, 48, 255)), (1, (255, 154, 84, 255)), (1, (255, 235, 241, 255)), (7, (255, 135, 53, 255)), (62, (255, 159, 93, 255)), (2, (255, 177, 124, 255)), (4, (255, 187, 210, 255)), (11, (255, 251, 248, 255)), (1, (255, 229, 211, 255)), (1, (255, 208, 176, 255)), (1, (255, 133, 50, 255)), (2, (255, 219, 231, 255)), (2, (255, 141, 63, 255)), (2, (255, 146, 71, 255)), (1, (255, 160, 95, 255)), (2, (255, 184, 135, 255)), (1, (255, 208, 175, 255)), (1, (255, 139, 61, 255)), (1, (255, 189, 211, 255)), (2, (255, 145, 69, 255)), (263, (255, 191, 147, 255)), (4, (255, 187, 141, 255)), (3, (255, 250, 252, 255)), (1, (255, 147, 72, 255)), (5, (255, 177, 203, 255)), (1, (255, 169, 109, 255)), (62, (255, 207, 174, 255))]
((163, 255), (73, 255), (14, 255), (255, 255))
(255, 127, 39, 255)
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80444]

(5) 圖像粘貼操作(paste)

Image.paste(im,box=None,maske=None)

使用im粘貼到原圖片中。注意:兩個圖片的mode和size要求一致,不一致可以使用convert()和resize()進(jìn)行調(diào)整。

# -*- coding:utf-8 -*-
from PIL import Image
rawimg = Image.open("qqtou.png")
print rawimg.size

im = Image.open("number.png")
print im.size

# rawimg的size和im的size要相同,不然不能匹配
# paste(用來粘貼的圖片,(位置坐標(biāo))),可以通過設(shè)置位置坐標(biāo)來確定粘貼圖片的位置
# 該方法沒有返回值,直接作用于原圖片
rawimg.paste(im,(75,-90))
rawimg.show()

(6) 各種put操作

Image.putalpha(alpha) - 添加多一層alpha層,沒看出具體效果

Image.putdata(data,scale=1.0,offset=0.0) - 添加一個像素序列到原圖像。

# -*- coding:utf-8 -*-
from PIL import Image

img = Image.open("qqtou.png")
img = img.convert("L")
img.show()

imgdata = list(img.getdata())
print imgdata

addlist = []
for i in range(len(imgdata)):
  if imgdata[i]>250:
    addlist.append(imgdata[i]-100)
  else:
    addlist.append(imgdata[i])


# putdata - 將一個序列添加進(jìn)原圖像,沒有返回值,直接作用在原圖像中
img.putdata(addlist)
img.show()

顯然,原始圖像(左圖)已經(jīng)發(fā)生了改變。

關(guān)于“Python中PIL庫有什么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI