今天在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/nowamagic_venv/Imaging-1.1.7/setup.py build_ext -i
檢查沒問題,可以執行安裝了:
python /root/nowamagic_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 http://www.linuxidc.com/Linux/2015-01/111870.htm
《Python核心編程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm
《Python開發技術詳解》.( 周偉,宗傑).[高清PDF掃描版+隨書視頻+代碼] http://www.linuxidc.com/Linux/2013-11/92693.htm
Python腳本獲取Linux系統信息 http://www.linuxidc.com/Linux/2013-08/88531.htm
在Ubuntu下用Python搭建桌面算法交易研究環境 http://www.linuxidc.com/Linux/2013-11/92534.htm
Python 語言的發展簡史 http://www.linuxidc.com/Linux/2014-09/107206.htm
Python 的詳細介紹:請點這裡
Python 的下載地址:請點這裡