CentOS 6.5 Python Image Library 配置

轉自:http://www.cnblogs.com/way_testlife/archive/2011/04/17/2019013.htmlhtml

PIL 下載:python

http://www.pythonware.com/products/pil/index.htmide

安裝 PIL函數

        $ tar xvfz Imaging-1.1.7.tar.gz
        $ cd Imaging-1.1.7
        $ python setup.py install

post

1. 簡介。學習

    圖像處理是一門應用很是廣的技術,而擁有很是豐富第三方擴展庫的 Python 固然不會錯過這一門盛宴。PIL (Python Imaging Library)是 Python 中最經常使用的圖像處理庫,目前版本爲 1.1.7,咱們能夠 在這裏 下載學習和查找資料。動畫

    Image 類是 PIL 庫中一個很是重要的類,經過這個類來建立實例能夠有直接載入圖像文件,讀取處理過的圖像和經過抓取的方法獲得的圖像這三種方法。spa

2. 使用。3d

    導入 Image 模塊。而後經過 Image 類中的 open 方法便可載入一個圖像文件。若是載入文件失敗,則會引發一個 IOError ;若無返回錯誤,則 open 函數返回一個 Image 對象。如今,咱們能夠經過一些對象屬性來檢查文件內容,即:code

1 >>> import Image
2  >>> im = Image.open("j.jpg")
3  >>> print im.format, im.size, im.mode
4 JPEG (440, 330) RGB

    這裏有三個屬性,咱們逐一瞭解。

        format : 識別圖像的源格式,若是該文件不是從文件中讀取的,則被置爲 None 值。

        size : 返回的一個元組,有兩個元素,其值爲象素意義上的寬和高。

        mode : RGB(true color image),此外還有,L(luminance),CMTK(pre-press image)。

    如今,咱們可使用一些在 Image 類中定義的方法來操做已讀取的圖像實例。好比,顯示最新載入的圖像:

1 >>>im.show()
2  >>>

    輸出原圖:

3. 函數概貌。

3.1    Reading and Writing Images : open( infilename ) , save( outfilename )

3.2    Cutting and Pasting and Merging Images :

        crop() : 從圖像中提取出某個矩形大小的圖像。它接收一個四元素的元組做爲參數,各元素爲(left, upper, right, lower),座標系統的原點(0, 0)是左上角。

        paste() : 

        merge() :

複製代碼
1 >>> box = (100, 100, 200, 200)
2  >>> region = im.crop(box)
3  >>> region.show()
4  >>> region = region.transpose(Image.ROTATE_180)
5  >>> region.show()
6  >>> im.paste(region, box)
7  >>> im.show()
複製代碼

    其效果圖爲:

    旋轉一幅圖片:

複製代碼
1 def roll(image, delta):
2 "Roll an image sideways"
3
4 xsize, ysize = image.size
5
6 delta = delta % xsize
7 if delta == 0: return image
8
9 part1 = image.crop((0, 0, delta, ysize))
10 part2 = image.crop((delta, 0, xsize, ysize))
11 image.paste(part2, (0, 0, xsize-delta, ysize))
12 image.paste(part1, (xsize-delta, 0, xsize, ysize))
13
14 return image
複製代碼
3.3    幾何變換。

3.3.1    簡單的幾何變換。

複製代碼
1 >>>out = im.resize((128, 128)) #
2  >>>out = im.rotate(45) #逆時針旋轉 45 度角。
3  >>>out = im.transpose(Image.FLIP_LEFT_RIGHT) #左右對換。
4  >>>out = im.transpose(Image.FLIP_TOP_BOTTOM) #上下對換。
5  >>>out = im.transpose(Image.ROTATE_90) #旋轉 90 度角。
6  >>>out = im.transpose(Image.ROTATE_180) #旋轉 180 度角。
7 >>>out = im.transpose(Image.ROTATE_270) #旋轉 270 度角。
複製代碼

    各個調整以後的圖像爲:

    圖片1:

    圖片2:

    圖片3:

    圖片4:

3.3.2    色彩空間變換。

    convert() : 該函數能夠用來將圖像轉換爲不一樣色彩模式。

3.3.3    圖像加強。

    Filters : 在 ImageFilter 模塊中可使用 filter 函數來使用模塊中一系列預約義的加強濾鏡。

 

1 >>> import ImageFilter
2 >>> imfilter = im.filter(ImageFilter.DETAIL)
3 >>> imfilter.show()
3.4    序列圖像。

    即咱們常見到的動態圖,最多見的後綴爲 .gif ,另外還有 FLI / FLC 。PIL 庫對這種動畫格式圖也提供了一些基本的支持。當咱們打開這類圖像文件時,PIL 自動載入圖像的第一幀。咱們可使用 seek 和 tell 方法在各幀之間移動。

 

複製代碼
1 import Image
2 im.seek(1) # skip to the second frame
3
4 try:
5 while 1:
6 im.seek( im.tell() + 1)
7 # do something to im
8 except EOFError:
9 pass
複製代碼

3.5    更多關於圖像文件的讀取。

    最基本的方式:im = Image.open("filename")

    類文件讀取:fp = open("filename", "rb"); im = Image.open(fp)

    字符串數據讀取:import StringIO; im = Image.open(StringIO.StringIO(buffer))

    從歸檔文件讀取:import TarIO; fp = TarIo.TarIO("Image.tar", "Image/test/lena.ppm"); im = Image.open(fp)

基本的 PIL 目前就練習到這裏。其餘函數的功能可點擊 這裏 進一步閱讀。

相關文章
相關標籤/搜索