Pillow中最重要的類就是Image,該類存在於同名的模塊中。能夠經過如下幾種方式實例化:從文件中讀取圖片,處理其餘圖片獲得,或者直接建立一個圖片。python
使用Image模塊中的open函數打開一張圖片:express
1
2
3
4
5
6
|
>>> 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圖像。windows
若是文件不能打開,則拋出IOError異常。bash
當有一個Image對象時,能夠用Image類的各個方法進行處理和操做圖像,例如顯示圖片:less
1
|
>>> im.show()
|
ps:標準版本的show()方法不是頗有效率,由於它先將圖像保存爲一個臨時文件,而後使用xv進行顯示。若是沒有安裝xv,該函數甚至不能工做。可是該方法很是便於debug和test。(windows中應該調用默認圖片查看器打開)ide
Pillow庫支持至關多的圖片格式。直接使用Image模塊中的open()函數讀取圖片,而沒必要先處理圖片的格式,Pillow庫自動根據文件決定格式。函數
Image模塊中的save()函數能夠保存圖片,除非你指定文件格式,那麼文件名中的擴展名用來指定文件格式。性能
1
2
3
4
5
6
7
8
9
10
11
|
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函數的第二個參數能夠用來指定圖片格式,若是文件名中沒有給出一個標準的圖像格式,那麼第二個參數是必須的。ui
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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等數據,餘下數據直到須要時才處理。spa
這意味着打開文件很是快,與文件大小和壓縮格式無關。下面的程序用來快速肯定圖片屬性:
1
2
3
4
5
6
7
8
9
|
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()方法能夠從圖片中提取一個子矩形
從圖片中複製子圖像
1
2
3
|
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。
處理子圖,粘貼回原圖
1
2
|
region
=
region.transpose(Image.ROTATE_180)
im.paste(region, box)
|
將子圖paste回原圖時,子圖的region必須和給定box的region吻合。該region不能超過原圖。而原圖和region的mode不須要匹配,Pillow會自動處理。
另外一個例子
1
2
3
4
5
6
7
8
9
10
11
12
|
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
|
1
2
|
r, g, b
=
im.split()
im
=
Image.merge(
"RGB"
, (b, g, r))
|
對於單通道圖片,split()返回圖像自己。爲了處理單通道圖片,必須先將圖片轉成RGB。
Image類有resize()、rotate()和transpose()、transform()方法進行幾何變換。
1
2
|
out
=
im.resize((
128
,
128
))
out
=
im.rotate(
45
)
# 順時針角度表示
|
置換圖像
1
2
3
4
5
|
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()方法
1
|
im
=
Image.
open
(
'lena.ppm'
).convert(
'L'
)
|
Filter
ImageFilter模塊包含不少預約義的加強filters,經過filter()方法使用
應用filters
1
2
|
from
PIL
import
ImageFilter
out
=
im.
filter
(ImageFilter.DETAIL)
|
像素點處理
point()方法經過一個函數或者查詢表對圖像中的像素點進行處理(例如對比度操做)。
1
2
|
# multiply each pixel by 1.2
out
=
im.point(
lambda
i: i
*
1.2
)
|
上述方法能夠利用簡單的表達式進行圖像處理,經過組合point()和paste()還能選擇性地處理圖片的某一區域。
1
2
3
4
5
6
7
8
9
10
11
|
# 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的語句:
1
|
mask
=
source[R].point(
lambda
i: i <
100
and
255
)
|
該句能夠用下句表示
1
|
imout
=
im.point(
lambda
i: expression
and
255
)
|
若是expression爲假則返回expression的值爲0(由於and語句已經能夠得出結果了),不然返回255。(mask參數用法:當爲0時,保留當前值,255爲使用paste進來的值,中間則用於transparency效果)
對其餘高級圖片加強,應該使用ImageEnhance模塊 。一旦有一個Image對象,應用ImageEnhance對象就能快速地進行設置。 可使用如下方法調整對比度、亮度、色平衡和銳利度。
1
2
3
|
from
PIL
import
ImageEnhance
enh
=
ImageEnhance.Contrast(im)
enh.enhance(
1.3
).show(
"30% more contrast"
)
|
Pillow支持一些動態圖片的格式如FLI/FLC,GIF和其餘一些處於實驗階段的格式。TIFF文件一樣能夠包含數幀圖像。
當讀取動態圖時,PIL自動讀取動態圖的第一幀,可使用seek和tell方法讀取不一樣幀。
1
2
3
4
5
6
7
8
9
|
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到下一幀。爲了倒回以前,必須從新打開文件。
或者可使用下述迭代器類
動態圖迭代器類
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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()函數已經足夠平常使用。該函數的參數也能夠是一個文件對象。
1
2
|
import
StringIO
im
=
Image.
open
(StringIO.StringIO(
buffer
))
|
從tar文件中讀取
1
2
3
|
from
PIL
import
TarIO
fp
=
TarIO.TarIO(
"Imaging.tar"
,
"Imaging/test/lena.ppm"
)
im
=
Image.
open
(fp)
|
draft()方法容許在不讀取文件內容的狀況下儘量(可能不會徹底等於給定的參數)地將圖片轉成給定模式和大小,這在生成縮略圖的時候很是有效(速度要求比質量高的場合)。
draft模式
1
2
3
4
5
|
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)
|
www.qytang.com/
http://www.qytang.com/cn/list/29/
http://www.qytang.com/cn/list/28/610.htm
http://www.qytang.com/cn/list/28/595.htm
http://www.qytang.com/cn/list/28/583.htm
http://www.qytang.com/cn/list/28/582.htm
http://www.qytang.com/cn/list/28/576.htm
http://www.qytang.com/cn/list/28/523.htm
http://www.qytang.com/cn/list/28/499.htm
http://www.qytang.com/cn/list/28/488.htm
http://www.qytang.com/cn/list/28/466.htm
http://www.qytang.com/cn/list/28/463.htm
http://www.qytang.com/cn/list/28/458.htm
http://www.qytang.com/cn/list/28/455.htm
http://www.qytang.com/cn/list/28/447.htm