Python影像處理庫:Pillow 初級教程

pythontab發表於2015-12-03

Image類

Pillow中最重要的類就是Image,該類存在於同名的模組中。可以透過以下幾種方式例項化:從檔案中讀取圖片,處理其他圖片得到,或者直接建立一個圖片。

使用Image模組中的open函式開啟一張圖片:

>>> from PIL import Image
>>> im = Image.open("lena.ppm")
如果開啟成功,返回一個Image物件,可以透過物件屬性檢查檔案內容
>>> from __future__ import print_function
>>> print(im.format, im.size, im.mode)
PPM (512, 512) RGB

format屬性定義了影像的格式,如果影像不是從檔案開啟的,那麼該屬性值為None;size屬性是一個tuple,表示影像的寬和高(單位為畫素);mode屬性為表示影像的模式,常用的模式為:L為灰度圖,RGB為真彩色,CMYK為pre-press影像。

如果檔案不能開啟,則丟擲IOError異常。

當有一個Image物件時,可以用Image類的各個方法進行處理和操作影像,例如顯示圖片:

>>> im.show()

ps:標準版本的show()方法不是很有效率,因為它先將影像儲存為一個臨時檔案,然後使用xv進行顯示。如果沒有安裝xv,該函式甚至不能工作。但是該方法非常便於debug和test。(windows中應該呼叫預設圖片檢視器開啟)

讀寫圖片

Pillow庫支援相當多的圖片格式。直接使用Image模組中的open()函式讀取圖片,而不必先處理圖片的格式,Pillow庫自動根據檔案決定格式。

Image模組中的save()函式可以儲存圖片,除非你指定檔案格式,那麼檔名中的副檔名用來指定檔案格式。

圖片轉成jpg格式

from __future__ import print_function
import os, sys
from PIL import Image
for infile in sys.argv[1:]:
    f, e = os.path.splitext(infile)
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            Image.open(infile).save(outfile)
        except IOError:
            print("cannot convert", infile)

save函式的第二個引數可以用來指定圖片格式,如果檔名中沒有給出一個標準的影像格式,那麼第二個引數是必須的。

建立縮圖

from __future__ import print_function
import os, sys
from PIL import Image
size = (128, 128)
for infile in sys.argv[1:]:
    outfile = os.path.splitext(infile)[0] + ".thumbnail"
    if infile != outfile:
        try:
            im = Image.open(infile)
            im.thumbnail(size)
            im.save(outfile, "JPEG")
        except IOError:
            print("cannot create thumbnail for", infile)

必須指出的是除非必須,Pillow不會解碼或raster資料。當你開啟一個檔案,Pillow透過檔案頭確定檔案格式,大小,mode等資料,餘下資料直到需要時才處理。

這意味著開啟檔案非常快,與檔案大小和壓縮格式無關。下面的程式用來快速確定圖片屬性:

確定圖片屬性

from __future__ import print_function
import sys
from PIL import Image
for infile in sys.argv[1:]:
    try:
        with Image.open(infile) as im:
            print(infile, im.format, "%dx%d" % im.size, im.mode)
    except IOError:
        pass

裁剪、貼上、與合併圖片

Image類包含還多操作圖片區域的方法。如crop()方法可以從圖片中提取一個子矩形

從圖片中複製子影像

box = im.copy() #直接複製影像
box = (100, 100, 400, 400)
region = im.crop(box)

區域由4-tuple決定,該tuple中資訊為(left, upper, right, lower)。 Pillow左邊系統的原點(0,0)為圖片的左上角。座標中的數字單位為畫素點,所以上例中擷取的圖片大小為300*300畫素^2。

處理子圖,貼上回原圖

region = region.transpose(Image.ROTATE_180)
im.paste(region, box)

將子圖paste回原圖時,子圖的region必須和給定box的region吻合。該region不能超過原圖。而原圖和region的mode不需要匹配,Pillow會自動處理。

另一個例子

Rolling an image
def roll(image, delta):
    "Roll an image sideways"
    image = image.copy() #複製影像
    xsize, ysize = image.size
    delta = delta % xsize
    if delta == 0: return image
    part1 = image.crop((0, 0, delta, ysize))
    part2 = image.crop((delta, 0, xsize, ysize))
    image.paste(part2, (0, 0, xsize-delta, ysize))
    image.paste(part1, (xsize-delta, 0, xsize, ysize))
    return image

分離和合並通道

r, g, b = im.split()
im = Image.merge("RGB", (b, g, r))

對於單通道圖片,split()返回影像本身。為了處理單通道圖片,必須先將圖片轉成RGB。

幾何變換

Image類有resize()、rotate()和transpose()、transform()方法進行幾何變換。

簡單幾何變換

out = im.resize((128, 128))
out = im.rotate(45) # 順時針角度表示

置換影像

out = im.transpose(Image.FLIP_LEFT_RIGHT)
out = im.transpose(Image.FLIP_TOP_BOTTOM)
out = im.transpose(Image.ROTATE_90)
out = im.transpose(Image.ROTATE_180)
out = im.transpose(Image.ROTATE_270)

transpose()和象的rotate()沒有效能差別。

更通用的影像變換方法可以使用transform()

模式轉換

convert()方法

模式轉換

im = Image.open('lena.ppm').convert('L')

影像增強

Filter

ImageFilter模組包含很多預定義的增強filters,透過filter()方法使用

應用filters

from PIL import ImageFilter
out = im.filter(ImageFilter.DETAIL)

畫素點處理

point()方法透過一個函式或者查詢表對影像中的畫素點進行處理(例如對比度操作)。

畫素點變換

# multiply each pixel by 1.2
out = im.point(lambda i: i * 1.2)

上述方法可以利用簡單的表示式進行影像處理,透過組合point()和paste()還能選擇性地處理圖片的某一區域。

處理單獨通道

# split the image into individual bands
source = im.split()
R, G, B = 0, 1, 2
# select regions where red is less than 100
mask = source[R].point(lambda i: i < 100 and 255)
# process the green band
out = source[G].point(lambda i: i * 0.7)
# paste the processed band back, but only where red was < 100
source[G].paste(out, None, mask)
# build a new multiband image
im = Image.merge(im.mode, source)

注意到建立mask的語句:

mask = source[R].point(lambda i: i < 100 and 255)

該句可以用下句表示

imout = im.point(lambda i: expression and 255)

如果expression為假則返回expression的值為0(因為and語句已經可以得出結果了),否則返回255。(mask引數用法:當為0時,保留當前值,255為使用paste進來的值,中間則用於transparency效果)

高階圖片增強

對其他高階圖片增強,應該使用ImageEnhance模組 。一旦有一個Image物件,應用ImageEnhance物件就能快速地進行設定。 可以使用以下方法調整對比度、亮度、色平衡和銳利度。

影像增強

from PIL import ImageEnhance
enh = ImageEnhance.Contrast(im)
enh.enhance(1.3).show("30% more contrast")

動態圖

Pillow支援一些動態圖片的格式如FLI/FLC,GIF和其他一些處於實驗階段的格式。TIFF檔案同樣可以包含數幀影像。

當讀取動態圖時,PIL自動讀取動態圖的第一幀,可以使用seek和tell方法讀取不同幀。

from PIL import Image
im = Image.open("animation.gif")
im.seek(1) # skip to the second frame
try:
    while 1:
        im.seek(im.tell()+1)
        # do something to im
except EOFError:
    pass # end of sequence

當讀取到最後一幀時,Pillow丟擲EOFError異常。

當前版本只允許seek到下一幀。為了倒回之前,必須重新開啟檔案。

或者可以使用下述迭代器類

動態圖迭代器類

class ImageSequence:
    def __init__(self, im):
        self.im = im
    def __getitem__(self, ix):
        try:
            if ix:
                self.im.seek(ix)
            return self.im
        except EOFError:
            raise IndexError # end of sequence
for frame in ImageSequence(im):
    # ...do something to frame...
Postscript Printing

Pillow允許透過Postscript Printer在圖片上新增images、text、graphics。

Drawing Postscript
from PIL import Image
from PIL import PSDraw
im = Image.open("lena.ppm")
title = "lena"
box = (1*72, 2*72, 7*72, 10*72) # in points
ps = PSDraw.PSDraw() # default is sys.stdout
ps.begin_document(title)
# draw the image (75 dpi)
ps.image(box, im, 75)
ps.rectangle(box)
# draw centered title
ps.setfont("HelveticaNarrow-Bold", 36)
w, h, b = ps.textsize(title)
ps.text((4*72-w/2, 1*72-h), title)
ps.end_document()

ps:textsize不能用,有誰知道嗎

更多讀取圖片方法

之前說到Image模組的open()函式已經足夠日常使用。該函式的引數也可以是一個檔案物件。

從string中讀取

import StringIO
im = Image.open(StringIO.StringIO(buffer))

從tar檔案中讀取

from PIL import TarIO
fp = TarIO.TarIO("Imaging.tar", "Imaging/test/lena.ppm")
im = Image.open(fp)

草稿模式

draft()方法允許在不讀取檔案內容的情況下儘可能(可能不會完全等於給定的引數)地將圖片轉成給定模式和大小,這在生成縮圖的時候非常有效(速度要求比質量高的場合)。

draft模式

from __future__ import print_function
im = Image.open(file)
print("original =", im.mode, im.size)
im.draft("L", (100, 100))
print("draft =", im.mode, im.size)


相關文章