Python影像處理庫(PIL)的安裝與簡單使用

來源:互聯網
上載者:User

Python影像處理庫(PIL)的安裝與簡單使用

今天在Python運行環境的伺服器弄一個有關影像處理的程式時報這樣的錯:

NameError: global name 'Image' is not defined

import Image 了下,發現原來 Python 並沒有內建影像處理庫,需要獨立安裝……查了下,Python常用的影像處理庫叫PIL,可以使用 pip 安裝,不錯~於是在 用virtualenv 裡敲入 pip install PIL。

安裝很快完成,於是愉悅地重新整理,等待程式的通過,結果又報錯:

IOError: decoder jpeg not available

Google了下,發現通過 pip 安裝的 PIL 不會安裝 jpeg 的解碼器……檢查了下安裝日誌,也有這樣的說明:

--------------------------------------------------------------------
PIL 1.1.7 SETUP SUMMARY
--------------------------------------------------------------------
version      1.1.7
platform      linux2 2.7.5 (default, Sep 18 2013, 09:53:07)
    [GCC 4.1.2 20080704 (Red Hat 4.1.2-54)]
--------------------------------------------------------------------
*** TKINTER support not available
*** JPEG support not available
*** ZLIB (PNG/ZIP) support not available
*** FREETYPE2 support not available
*** LITTLECMS support not available
--------------------------------------------------------------------
To add a missing option, make sure you have the required
library, and set the corresponding ROOT variable in the
setup.py script.

JPEG support not available…… jpg都不支援,這是鬧哪樣……

於是只得手動安裝了:

wget http://effbot.org/downloads/Imaging-1.1.7.tar.gz

tar xvfz Imaging-1.1.7.tar.gz

下載並解壓成功之後,到解壓目錄,找到 Imaging-1.1.7/setup.py 這個檔案,修改下面幾行代碼(預設TCL_ROOT的設定為NONE,這裡要傳到系統庫的路徑才行):

TCL_ROOT = "/usr/lib64/"
JPEG_ROOT = "/usr/lib64/"
ZLIB_ROOT = "/usr/lib64/"
TIFF_ROOT = "/usr/lib64/"
FREETYPE_ROOT = "/usr/lib64/"
LCMS_ROOT = "/usr/lib64/"

再進行安裝前的檢查:

python /root/bkjia_venv/Imaging-1.1.7/setup.py build_ext -i

檢查沒問題,可以執行安裝了:

python /root/bkjia_venv/Imaging-1.1.7/setup.py install

安裝成功:

--------------------------------------------------------------------
PIL 1.1.7 SETUP SUMMARY
--------------------------------------------------------------------
version      1.1.7
platform      linux2 2.7.5 (default, Sep 18 2013, 09:53:07)
              [GCC 4.1.2 20080704 (Red Hat 4.1.2-54)]
--------------------------------------------------------------------
*** TKINTER support not available
--- JPEG support available
--- ZLIB (PNG/ZIP) support available
--- FREETYPE2 support available
*** LITTLECMS support not available
--------------------------------------------------------------------

現在 jpg 已經被支援了,程式也執行成功,這裡簡單記錄一下過程,方便後來者。順便附帶測試程式,用 Tornado 上傳圖片並產生縮圖:

import time
import tempfile
import Image

class AsciiImageProcessHandler(tornado.web.RequestHandler):
    def post(self):

        if self.request.files:
            for f in self.request.files['image']:
                rawname = f['filename']
                dstname = str(int(time.time()))+'.'+rawname.split('.').pop()
                thbname = "thumb_"+dstname

                self.write( dstname )

                tf = tempfile.NamedTemporaryFile()
                tf.write(f['body'])
                tf.seek(0)

                # create normal file
                # img = Image.open(src)
                img = Image.open(tf.name)
                img.thumbnail((920,920),resample=1)
                img.save("./static/upload/asciiimg/"+dstname)

                # create thumb file
                img.thumbnail((100,100),resample=1)
                img.save("./static/upload/asciiimg_tn/"+thbname)
 
                tf.close()

--------------------------------------分割線 --------------------------------------

CentOS上源碼安裝Python3.4 

《Python核心編程 第二版》.(Wesley J. Chun ).[高清PDF中文版]

《Python開發技術詳解》.( 周偉,宗傑).[高清PDF掃描版+隨書視頻+代碼]

Python指令碼擷取Linux系統資訊

在Ubuntu下用Python搭建案頭演算法交易研究環境

Python 語言的發展簡史

Python 的詳細介紹:請點這裡
Python 的:請點這裡

本文永久更新連結地址: 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.