2021-05-12 14:32:11
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/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
相關文章