python的Image模組
阿新 • • 發佈:2018-11-10
原文連結: https://www.cnblogs.com/DjangoBlog/p/3557744.html
Image 模組
Image 模組提供了同名的類用來表示PIL的影象。Image模組還提供了許多工廠(factory)函式,包塊從檔案載入影象的函式,以及建立新影象的函式。
例子
下面的指令碼載入了一個影象,並把它旋轉了45度,然後呼叫外部的檢視器(通常在Unix下是xv,Windows下是paint)。
開啟,旋轉,和顯示影象(使用預設的檢視器)
下面的指令碼為當前目錄下所以的JPEG影象建立漂亮128x128的縮圖。 建立縮圖from PIL import Image im = Image.open("bride.jpg") im.rotate(45).show()
from PIL import Image import glob, os size = 128, 128 for infile in glob.glob("*.jpg"): file, ext = os.path.splitext(infile) im = Image.open(infile) im.thumbnail(size, Image.ANTIALIAS) im.save(file + ".thumbnail", "JPEG")
函式 new
Image.new(mode, size) => image
Image.new(mode, size, color) => image
以指定的模式和大小建立一個新影象。大小以2元元組的形式給出。給colour賦單個值,表示要建立單波段影象,元組表示建立多波段影象(每個波段一個值)。如果忽略colour引數,影象將以黑色填充。如果colour設為None,影象不會被初始化。open
Image.open(infile) => image
Image.open(infile, mode) => image
開啟並識別給定影象檔案。這是一個偷懶的操作;真正的影象資料只有到處理的時候才會被讀入(呼叫load函式強制載入)。如果給出了模式(mode)引數,它必須設為“r”。 要開啟影象,即可以使用字串(表示檔名)也可以使用檔案物件。對後一種情況,檔案物件必須實現了read,seek,和 tell 方法,並以二進位制模式開啟。blend
Image.blend(image1, image2, alpha) => image
通過使用alpha常量,在影象進行差值操作,建立新影象。兩個影象必須具有相同的大小和模式。 out = image1 * (1.0 - alpha) + image2 * alpha (注:沒有成功) 如果設定alpha為0.0,將返回第一個影象的拷貝。如果設定alpha為1.0,將返回第二個影象的拷貝。對alpha的值沒有限制。必要的話,結果會被剪裁,以適合允許的輸出範圍。composite
Image.composite(image1, image2, mask) => image
使用遮罩(mask)作為alpha,通過在兩個影象之間進行插值來建立一個新影象。遮罩影象的模式可以是“1”,“L”,或者“RGBA”。所有的影象的大小必須有相同。eval
Image.eval_r(image, function) => image
把函式(function)(應該接收一個引數)應用到所給影象的每一個畫素。如果影象有多個波段,相同的函式會應用到每一個波段。注意,該函式對每一個可能的畫素值只計算一次,所有不能使用隨機元件(components)或者其它發生器(generators)。frombuffer
Image.frombuffer(mode, size, data) => image
(PIL1.1.4新增)。使用標準的“raw”解碼器,把來自字串或者緩衝區(buffer)物件的影象資料建立為一個影象記憶體(image memory)。對於某些模式,影象記憶體會和原來的緩衝區共享記憶體(這意味著對原始緩衝區物件的修改會影響影象)。不是所有的模式都能共享記憶體;支援共享記憶體的模式包括:“L”,“RGBX”,“RGBA”和“CMYK”。對其其它模式,這個函式的作用與fromstring函式類似。 注意:1.1.6版中,預設的方向與fromstring的不同。這些可能會在未來的版本中發生變化,所以為了最大的相容性,建議在使用“raw”解碼器的時候給出所有的引數。 im = Image.frombuffer(mode, size, data, "raw", mode, 0, 1)Image.frombuffer(mode, size, data, decoder, parameters) => image 與呼叫fromstring 相同。fromstring
Image.fromstring(mode, size, data) => image
使用標準的“raw”解碼器從來自字串的畫素資料建立一個影象記憶體。 Image.fromstring(mode, size, data, decoder, parameters) => image 也一樣,但是允許你使用PIL支援的任何畫素解碼器。關於可用解碼器的更多資訊,參見Writing Your Own File Decoder節 注意,這個函式只對畫素資料解碼,而不是整個影象。如果字串中包含了一個完整的影象檔案,可以使用StringIO物件對它進行處理,並使用open函式載入影象。merge
Image.merge(mode, bands) => image
從幾個單波段影象建立一個新影象。bands引數是包含影象的元組或列表,一個影象對應模式中描述的一個波段。所有波段的影象必須有相同的大小。 方法 一個Image類的例項具有下列方法。除非另外指出,所有的方法都返回一個新的Image類的例項,包含處理過的影象資料。convert
im.convert(mode) => image
返回影象轉換後的副本 如果原始影象是調色盤影象,這個函式通過調色盤轉換畫素。忽略mode引數,會自動選擇一個模式,以保證所有的影象資訊和調色盤資訊在沒有調色盤的時候也能表示出來。 從彩色影象轉換到黑白影象時,影象庫使用ITU-R 601-2 luma轉換: L = R * 299/1000 + G * 587/1000 + B * 114/1000在把影象轉換為二值圖(bilevel image)(模式“1”)時,源影象首先被轉換為黑白圖。然後在結果中,值大於127的畫素點被設定為白色,影象抖動(and the image is dithered)。使用point方法可以改變閾值。 im.convert(mode, matrix) => image 使用轉換矩陣,把一個 "RGB" 影象轉換為 "L" 或者 "RGB" 影象。其中矩陣是一個4元或16元元組。 下面的例子把一個RGB影象轉換(根據ITU-R 709進行線性校正,using the D65 luminant)到CIE XYZ顏色空間:Convert RGB to XYZ
rgb2xyz = (
0.412453, 0.357580, 0.180423, 0,
0.212671, 0.715160, 0.072169, 0,
0.019334, 0.119193, 0.950227, 0 )
out = im.convert("RGB", rgb2xyz)