Pillow使用Image篇的使用
安裝Pillow
pip install pillow
構(gòu)建圖像
Image.open(fp, mode ='r' ):打開(kāi)圖片文件,返回一個(gè)Image對(duì)象
- fp:圖片路徑
- mode:模式。如果給出,必須是r
from PIL import Image im = Image.open(path)
Image.alpha_composite(im1, im2):在im1對(duì)象上的透明層復(fù)合im2,返回一個(gè)Image對(duì)象
- im1:Image對(duì)象1
- im2:Image對(duì)象2
from PIL import Image im1 = Image.open(path1) im2 = Image.open(path2) im3 = Image.alpha_composite(im1,im2)
Image.blend(im1, im2, alpha):在兩個(gè)圖片對(duì)象之間進(jìn)行插值,返回一個(gè)Image對(duì)象
- im1:Image對(duì)象1
- im2:Image對(duì)象2
- alpha:透明圖
如果alpha為0.0,則返回第一個(gè)圖像的副本。如果alpha為1.0,則返回第二個(gè)圖像的副本,基本的算法如下:
out = image1 * (1.0 - alpha ) + image2 * alpha
Image.eval(image, *args):將函數(shù)應(yīng)用于給定圖像的中每一個(gè)像素。請(qǐng)注意,該函數(shù)對(duì)每個(gè)可能的像素值都進(jìn)行一次評(píng)估,因此您不能使用隨機(jī)組件或其他生成器。返回一個(gè)Image對(duì)象
- image:Image對(duì)象
- args:一個(gè)函數(shù)對(duì)象和該函數(shù)的一個(gè)取整參數(shù)
from PIL import Image def func(a): return a im1 = Image.open(path1) img = Image.eval(img1,func,1)
Image.merge(mode, bands):將一組單波段圖像合并成為一個(gè)多波段圖像。返回一個(gè)Image對(duì)象
- mode:用于輸出圖像的模式。支持的模式請(qǐng)看下方Pillow支持的模式表
- bands:輸出圖像中每個(gè)波段包含一個(gè)單波段圖像的序列
Image.new(mode, size, color=0):根據(jù)模式、大小和顏色創(chuàng)建一個(gè)新的Image對(duì)象。煩會(huì)一個(gè)Image對(duì)象
- mode:用于新圖像的模式。支持的模式請(qǐng)看下方Pillow支持的模式表
- size: 大小,元組類型,包含寬度與高度
- color:用于新圖像的顏色。傳入一個(gè)整數(shù)的單波段模式,或傳入一個(gè)元組的多波段模式,或者傳入一個(gè)ImageColor對(duì)象
from PIL import Image # 單個(gè)整數(shù)值 img = Image.new("RGBA",(1024,768),215) # 元組 img = Image.new("RGBA",(1024,768),(215,0,0) # ImageColor from PIL import ImageColor color = ImageColor.getrgb("#FF0000") img = Image.new("RGBA",(1024,768),color) img.show()
從上面代碼運(yùn)行結(jié)果顯示是一個(gè)紅色,1024*768的圖像
圖像對(duì)象
alpha_composite(im, dest=(0,0), source=(0,0)):在Image對(duì)象中符合im,效果與類方法alpha_composite相似。無(wú)返回值
- im:Image對(duì)象
- dest:指定此(目標(biāo))圖像左上角的可選的長(zhǎng)度為2的元組(左,上)
- source:蓋源圖像中左上角的長(zhǎng)度為2的元組(左,上)或源矩形邊界的長(zhǎng)度為4的元組(左,上,右,下)
- copy():復(fù)制此圖片
from PIL import Image img = Image.new("RGBA",(1024,768),215) img_copy = img.copy()
crop(box=None):返回此圖像的一個(gè)矩形區(qū)域,為一個(gè)Image對(duì)象
- box:裁剪矩形,為一個(gè)長(zhǎng)度為4的元組(左,上,右,下)
from PIL import Image img = Image.new("RGBA",(1024,768),215) img_copy = img.crop(box=(0,0,500,500))
draft(mode, size):配置圖像文件加載器,以便返回盡可能接近給定模式和大小的圖像版本,無(wú)返回值
- mode:模式
- size:大小
filter(filter):使用給定的過(guò)濾器過(guò)濾此圖像,返回一個(gè)Image對(duì)象
- filter:過(guò)濾器
getbands():獲取此圖像中每個(gè)波段名稱的元組。返回一個(gè)tuple
from PIL import Image img = Image.new("RGBA",(1024,768),215) print img.getbands() # ('R', 'G', 'B', 'A')
getbbox():計(jì)算圖像中非零區(qū)域的邊界框,返回一個(gè)tuple
from PIL import Image img = Image.new("RGBA",(1024,768),215) print img.getbbox() # (0, 0, 1024, 768)
getcolors(maxcolors=256):返回此圖像中使用的顏色列表,返回一個(gè)計(jì)算與像素元組組成的元組列表
- maxcolors: 最大顏色數(shù)量,超過(guò)此值,當(dāng)前方法將返回None
from PIL import Image img = Image.new("RGBA",(1024,768),215) print img.getcolors() # [(786432, (215, 0, 0, 0))]
getdata(band=None):以包含像素值的序列對(duì)象的形式返回此圖像的內(nèi)容。返回一個(gè)可迭代對(duì)象。
- band:波段,默認(rèn)是獲取所有。如果需要獲取單波段,傳入索引值(例如0從“RGB”圖像中獲得“R”波段)。
from PIL import Image img = Image.new("RGBA",(1024,768),215) for item in img.getdata(): print item # 打印結(jié)果: (215, 0, 0, 0) (215, 0, 0, 0) (215, 0, 0, 0) ...
getextrema():獲取每個(gè)波段的最小和最大像素值。對(duì)于單波段圖像,返回一個(gè)長(zhǎng)度為2的元組。對(duì)與多波段圖像,每個(gè)波段包含一個(gè)2元組。
from PIL import Image img = Image.new("RGBA",(1024,768),215) print img.getextrema() # ((215, 215), (0, 0), (0, 0), (0, 0))
getpalette():返回圖像的調(diào)色板,返回一個(gè)list對(duì)象。如果沒(méi)有調(diào)色板,則返回None
from PIL import Image img = Image.new("RGBA",(1024,768),215) print img.getpalette() # None
getpixel(xy):返回給定位置的像素值,返回一個(gè)tuple
- xy:位置,以(x,y)給出的坐標(biāo)。
from PIL import Image img = Image.new("RGBA",(1024,768),215) print img.getpixel((500,500)) # (215, 0, 0, 0)
histogram(mask=None, extrema=None):返回圖像的直方圖。直方圖以像素計(jì)數(shù)列表的形式返回,返回一個(gè)列表。
- mask:掩碼
- extrema:極值
paste(im, box=None, mask=None):將im圖像粘貼到此圖像上面。無(wú)返回值
- box:box可以是圖像左上角的長(zhǎng)度為2的元組(左,上)或長(zhǎng)度為4的元組(左,上,右,下)
- mask:掩碼
import os from PIL import Image path1 = os.path.join(os.getcwd(),"23.png") img1 = Image.open(path1) img = Image.new("RGBA",(1024,768),215) img1.paste(img) img1.show()
putdata(data, scale=1.0, offset=0.0):將像素?cái)?shù)據(jù)復(fù)制到此圖像上面。從圖像左上角開(kāi)始,直到圖像或序列結(jié)束,無(wú)返回值。比例和偏移值用于調(diào)整序列值:pixel = value * scale + offset。
- data:一個(gè)圖像數(shù)據(jù)序列
- scale:縮放比例值
- offset:偏移量值
from PIL import Image img = Image.new("RGBA",(1024,768),215) img_c = Image.new("RGBA",(1024,768),-100) img.putdata(img_c.getdata()) img.show()
putpalette(data, rawmode='RGB'):附加一個(gè)調(diào)色板到這個(gè)圖像。圖像的模式必須是P或者L。返回一個(gè)Image對(duì)象
- data:調(diào)色板序列
- rawmode:調(diào)色板的原始模式
from PIL import Image img = Image.new("P",(1024,768),215) img_c = Image.new("P",(1024,768),-100) img_c.putpalette(img.getpalette()) img_c.show()
quantize(colors=256, method=None, kmeans=0, palette=None):將圖像轉(zhuǎn)換為具有指定數(shù)量的顏色的P模式圖像,返回一個(gè)Image對(duì)象
- colors:所需顏色的數(shù)量,<=256
- method:0:中值切割,1:最大覆蓋率,2:快速八叉樹(shù),3:libimagequant
- kmeans:整數(shù)
- palette:量化給定的調(diào)色板
from PIL import Image img = Image.new("RGBA",(1024,768),215) img_q = img.quantize(colors=256,method=2) print img_q # <PIL.Image.Image image mode=P size=1024x768 at 0x2BF7E80>
resize(size, resample=0, box=None):返回此圖像的調(diào)整大小后的副本。返回一個(gè)Image對(duì)象
- size:以像素為單位的長(zhǎng)度為2的元組(寬度,高度)
- resample:重采樣濾波器??梢栽O(shè)置為:Image.NEAREST、Image.BOX、Image.BILINEAR、Image.HAMMING、Image.BICUBIC或者Image.LANCZOS。如果省略,或者圖像模式為1或者P,則設(shè)置Image.NEAREST。
- box:一個(gè)浮點(diǎn)數(shù)組成的長(zhǎng)度為4的元組,給出應(yīng)該縮放的源圖像的區(qū)域。值應(yīng)該在(0,0,寬度,高度)的矩形內(nèi)。
from PIL import Image img = Image.new("RGBA",(1024,768),215) img_r = img.resize(size=(500,500)) print img_r # <PIL.Image.Image image mode=RGBA size=500x500 at 0x37A6E80>
rotate(angle, resample=0, expand=0, ceter=None, translate=None):旋轉(zhuǎn)圖像,并返回旋轉(zhuǎn)后的圖像副本。返回Image對(duì)象
- angle:角度,逆時(shí)針旋轉(zhuǎn)
- resample:重采樣濾波器??梢允牵篒mage.NEAREST、Image.BILINEAR或者Image.BICUBIC。如果省略,或者圖像模式為1或者P,則設(shè)置Image.NEAREST。
- expand:是否展開(kāi)。如果為true,則展開(kāi)輸出圖像以使其足夠大以容納整個(gè)旋轉(zhuǎn)后的圖像。如果為false或省略,使輸出圖像的大小與輸入圖像相同。
- center:旋轉(zhuǎn)中心,長(zhǎng)度為2的元組(寬度,高度),原點(diǎn)是左上角,默認(rèn)是圖像的中心
- translate:旋轉(zhuǎn)后。一個(gè)長(zhǎng)度為2的元組(寬度,高度)
import os from PIL import Image path1 = os.path.join(os.getcwd(),"23.png") img1 = Image.open(path1) img_r = img1.rotate(45,Image.BICUBIC) img_r.show()
可以看到,圖像已經(jīng)逆時(shí)針旋轉(zhuǎn)了45度
save(fp, format=None, **params):保存圖像到給定的文件名下。如果沒(méi)有指定格式,則可以使用文件擴(kuò)展名來(lái)確定要使用的格式。無(wú)返回值
- fp:文件名或路徑
- format:可選的格式覆蓋
import os from PIL import Image path1 = os.path.join(os.getcwd(),"23.png") img1 = Image.open(path1) img_r = img1.rotate(45,Image.BICUBIC) img_r.save(os.path.join(os.getcwd(),"rotate.png"))
seek(frame):在這個(gè)序列文件中尋找給定的幀。如果您在序列結(jié)束之外尋找方法,則會(huì) 引發(fā)EOFError異常。當(dāng)序列文件被打開(kāi)時(shí),庫(kù)會(huì)自動(dòng)尋找0幀。無(wú)返回值
- frame:幀號(hào)。從0開(kāi)始
show(title=None, command=None):顯示這個(gè)圖像,此方法主要用于調(diào)試目的。無(wú)返回值
- title:在可能的情況下,用于圖像窗口的可選標(biāo)題。
- command:用于顯示圖像的命令
split():將圖像分割成單獨(dú)的波段。該方法從圖像中返回一個(gè)單獨(dú)的圖像的元組。例如,拆分“RGB”圖像會(huì)創(chuàng)建三個(gè)新圖像,每個(gè)圖像都包含原始波段(紅色,綠色,藍(lán)色)之一的副本。返回一個(gè)tuple
from PIL import Image path1 = os.path.join(os.getcwd(),"23.png") img1 = Image.open(path1) data = img1.split() print data # (<PIL.Image.Image image mode=L size=3500x3500 at 0x2DEC438>, <PIL.Image.Image image mode=L size=3500x3500 at 0x2DEC860>, <PIL.Image.Image image mode=L size=3500x3500 at 0x2DEC898>, <PIL.Image.Image image mode=L size=3500x3500 at 0x2DEC8D0>)
getchannel(channel):返回包含源圖像的單個(gè)通道的圖像。返回L模式的圖像,返回一個(gè)Image對(duì)象
- channel:返回什么頻道的圖像??梢允撬饕?“RGBA”的”R”通道為0)或通道名稱(“RGBA”的alpha通道為”A”)
from PIL import Image path1 = os.path.join(os.getcwd(),"23.png") img1 = Image.open(path1) im = img1.getchannel(0) 或者: im = img1.getchannel("R")
tell():獲取當(dāng)前的幀號(hào)。返回int
thumbnail(size, resample=3):將此圖像制作成縮略圖。該方法修改圖像以包含其本身的縮略圖版本,不大于給定尺寸。無(wú)返回值
- size:大小
- resample:重采樣濾波器??梢允牵篒mage.NEAREST、Image.BILINEAR、Image.BICUBIC或者Image.LANCZOS。如果省略,則默認(rèn)為Image.BICUBIC
from PIL import Image path1 = os.path.join(os.getcwd(),"23.png") img1 = Image.open(path1) img1.thumbnail(size=(500,500),resample=Image.BICUBIC) print img1 # <PIL.PngImagePlugin.PngImageFile image mode=RGBA size=500x500 at 0x311C3C8>
tobitmap(name='image'):返回轉(zhuǎn)換為X11位圖的圖像。此方法只使用于模式為1的圖像,返回一個(gè)str
- name:用于位圖變量的前綴名稱
from PIL import Image img = Image.new("1",(1024,768),215) data = img.tobitmap(name='abc') print data # 結(jié)果如下: """ #define abc_width 1024 #define abc_height 768 static char abc_bits[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, ... }; """
tobytes(encoder_name='raw', *args):以圖像作為字節(jié)對(duì)象返回。為一個(gè)str對(duì)象
transpose(method):旋轉(zhuǎn)或翻轉(zhuǎn)圖像,返回旋轉(zhuǎn)或翻轉(zhuǎn)后的圖像副本,一個(gè)Image對(duì)象
- method:可以是:Image.FLIP_LEFT_RIGHT、Image.FLIP_TOP_BOTTOM、Image.ROTATE_90、Image.ROTATE_180、Image.ROTATE_270、Image.TRANSPOSE或者Image.TRANSVERSE
from PIL import Image path1 = os.path.join(os.getcwd(),"23.png") img1 = Image.open(path1) im = img1.transpose(Image.FLIP_LEFT_RIGHT) im.show()
可以看出圖像已經(jīng)翻轉(zhuǎn)了
close():關(guān)閉文件指針
圖像對(duì)象屬性
filename:源文件的文件名或路徑。只有通過(guò)open方法構(gòu)建的圖像對(duì)象才具有此屬性
import os from PIL import Image path1 = os.path.join(os.getcwd(),"23.png") img = Image.open(path1) print img.filename # 、/aaa/bbb/ccc/23.png
format:源文件的圖片格式。對(duì)于由庫(kù)自身創(chuàng)建的圖像,此屬性值為None
import os from PIL import Image path1 = os.path.join(os.getcwd(),"23.png") img = Image.open(path1) print img.format # PNG img = Image.new("RGBA",(1024,768),215) print img.format # None
mode:圖像模式。這是一個(gè)字符串,指定圖像使用的像素格式。
from PIL import Image img = Image.new("RGBA",(1024,768),215) print img.mode # RGBA
size:圖像大小,以像素為單位。大小以長(zhǎng)度為2的元組(寬度,高度)給出。類型tuple
from PIL import Image img = Image.new("RGBA",(1024,768),215) print img.size # (1024, 768)
width:圖像寬度,以像素為單位。類型int
from PIL import Image img = Image.new("RGBA",(1024,768),215) print img.width # 1024
height:圖像高度,以像素為單位。類型int
from PIL import Image img = Image.new("RGBA",(1024,768),215) print img.height # 768
palette:調(diào)色板表。如果模式為P,這應(yīng)該是ImagePalette類的一個(gè)實(shí)例。否則為None
from PIL import Image img = Image.new("RGBA",(1024,768),215) print img.palette # None img = Image.new("P",(1024,768),215) print img.palette # <PIL.ImagePalette.ImagePalette object at 0x0000000002EF9828>
info:保存與圖像相關(guān)的數(shù)據(jù)的字典。這個(gè)字典被文件處理程序用來(lái)傳遞從文件讀取的各種非圖像信息。
import os from PIL import Image path1 = os.path.join(os.getcwd(),"23.png") img = Image.open(path1) print img.info # 結(jié)果如下: ''' { 'chromaticity': (0.31269, 0.32899, 0.63999, 0.33001, 0.3, 0.6, 0.15, 0.05999), 'icc_profile': 'xxxx/...', 'dpi': (300, 300) } ''' img = Image.new("RGBA",(1024,768),215) print img.info # {}
Pillow支持的模式表
模式 | 說(shuō)明 |
---|---|
1 | 1位像素,黑白,每字節(jié)一個(gè)像素存儲(chǔ) |
L | 8位像素,黑白 |
P | 8位像素,使用調(diào)色板映射到任何其他模式 |
RGB | 3x8位像素,真彩色 |
RGBA | 4×8位像素,帶透明度掩模的真彩色 |
CMYK | 4x8位像素,分色 |
YCbCr | 3x8位像素,彩色視頻格式 |
LAB | 3×8位像素,L * a * b顏色空間 |
HSV | 3x8位像素,色調(diào),飽和度,值顏色空間 |
I | 32位有符號(hào)整數(shù)像素 |
F | 32位浮點(diǎn)像素 |
更多關(guān)于Image的操作:http://pillow.readthedocs.io/en/latest/reference/Image.html
到此這篇關(guān)于Pillow使用Image篇的使用的文章就介紹到這了,更多相關(guān)Pillow Image篇內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Pandas如何對(duì)Categorical類型字段數(shù)據(jù)統(tǒng)計(jì)實(shí)戰(zhàn)案例
這篇文章主要介紹了Pandas如何對(duì)Categorical類型字段數(shù)據(jù)統(tǒng)計(jì)實(shí)戰(zhàn)案例,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08plt.title()中文無(wú)法顯示的問(wèn)題解決
本文主要介紹了plt.title()中文無(wú)法顯示的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04Python圖形繪制操作之正弦曲線實(shí)現(xiàn)方法分析
這篇文章主要介紹了Python圖形繪制操作之正弦曲線實(shí)現(xiàn)方法,涉及Python使用numpy模塊數(shù)值運(yùn)算及matplotlib.pyplot模塊進(jìn)行圖形繪制的相關(guān)操作技巧,需要的朋友可以參考下2017-12-12Python機(jī)器學(xué)習(xí)應(yīng)用之支持向量機(jī)的分類預(yù)測(cè)篇
最近完成的一個(gè)項(xiàng)目用到了SVM,之前也一直有聽(tīng)說(shuō)支持向量機(jī),知道它是機(jī)器學(xué)習(xí)中一種非常厲害的算法。利用將近一個(gè)星期的時(shí)間學(xué)習(xí)了一下支持向量機(jī),把原理推了一遍,感覺(jué)支持向量機(jī)確實(shí)挺厲害的,這篇文章帶你了解它2022-01-01Python通過(guò)隊(duì)列實(shí)現(xiàn)進(jìn)程間通信詳情
這篇文章主要介紹了Python通過(guò)隊(duì)列實(shí)現(xiàn)進(jìn)程間通信詳情文章通過(guò)提出問(wèn)題:在多進(jìn)程中,每個(gè)進(jìn)程之間是什么關(guān)系展開(kāi)主題相關(guān)內(nèi)容,感興趣的朋友可以參考一下2022-06-06Python實(shí)現(xiàn)批量上傳本地maven庫(kù)到nexus
這篇文章主要為大家詳細(xì)介紹了如何使用Python實(shí)現(xiàn)批量上傳本地maven庫(kù)到nexus,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的小伙伴可以參考下2024-01-01python?Pangu自動(dòng)美化中文排版工具使用探索
這篇文章主要為大家介紹了python?Pangu自動(dòng)美化中文排版工具使用探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01