python PIL except: IOError: decoder jpeg not available

今天在Python运行环境的服务器弄一个有关图像处理的程序时报这样的错:

1NameError: global name 'Image' is not defined

import Image 了下,发现原来 Python 并没有自带图像处理库,需要独立安装……查了下,Python常用的图像处理库叫PIL,可以使用 pip 安装,不错~于是在 用virtualenv 里敲入 pip install PIL。

安装很快完成,于是愉悦地刷新,等待程序的通过,结果又报错:

1IOError: decoder jpeg not available

Google了下,发现通过 pip 安装的 PIL 不会安装 jpeg 的解码器……检查了下安装日志,也有这样的说明:

01--------------------------------------------------------------------
02PIL 1.1.7 SETUP SUMMARY
03--------------------------------------------------------------------
04version       1.1.7
05platform      linux2 2.7.5 (default, Sep 18 201309:53:07)
06              [GCC 4.1.2 20080704 (Red Hat 4.1.2-54)]
07--------------------------------------------------------------------
08*** TKINTER support not available
09*** JPEG support not available
10*** ZLIB (PNG/ZIP) support not available
11*** FREETYPE2 support not available
12*** LITTLECMS support not available
13--------------------------------------------------------------------
14To add a missing option, make sure you have the required
15library, and set the corresponding ROOT variable in the
16setup.py script.

JPEG support not available…… jpg都不支持,这是闹哪样……

于是只得手动安装了:

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

下载并解压成功之后,到解压目录,找到 Imaging-1.1.7/setup.py 这个文件,修改下面几行代码(默认TCL_ROOT的设置为NONE,这里要传到系统库的路径才行):

1TCL_ROOT = "/usr/lib64/"
2JPEG_ROOT = "/usr/lib64/"
3ZLIB_ROOT = "/usr/lib64/"
4TIFF_ROOT = "/usr/lib64/"
5FREETYPE_ROOT = "/usr/lib64/"
6LCMS_ROOT = "/usr/lib64/"

再进行安装前的检查:

1python /root/nowamagic_venv/Imaging-1.1.7/setup.py build_ext -i

检查没问题,可以执行安装了:

1python /root/nowamagic_venv/Imaging-1.1.7/setup.py install

安装成功:

01--------------------------------------------------------------------
02PIL 1.1.7 SETUP SUMMARY
03--------------------------------------------------------------------
04version       1.1.7
05platform      linux2 2.7.5 (default, Sep 18 201309:53:07)
06              [GCC 4.1.2 20080704 (Red Hat 4.1.2-54)]
07--------------------------------------------------------------------
08*** TKINTER support not available
09--- JPEG support available
10--- ZLIB (PNG/ZIP) support available
11--- FREETYPE2 support available
12*** LITTLECMS support not available
13--------------------------------------------------------------------

现在 jpg 已经被支持了,程序也执行成功,这里简单记录一下过程,方便后来者。顺便附带测试程序,用 Tornado 上传图片并生成缩略图:

01import time
02import tempfile
03import Image
04 
05class AsciiImageProcessHandler(tornado.web.RequestHandler):
06    def post(self):
07 
08        if self.request.files:
09            for in self.request.files['image']:
10                rawname = f['filename']
11                dstname =str(int(time.time()))+'.'+rawname.split('.').pop()
12                thbname = "thumb_"+dstname
13 
14                self.write( dstname )
15 
16                tf = tempfile.NamedTemporaryFile()
17                tf.write(f['body'])
18                tf.seek(0)
19 
20                # create normal file
21                # img = Image.open(src)
22                img = Image.open(tf.name)
23                img.thumbnail((920,920),resample=1)
24                img.save("./static/upload/asciiimg/"+dstname)
25 
26                # create thumb file
27                img.thumbnail((100,100),resample=1)
28                img.save("./static/upload/asciiimg_tn/"+thbname)
29  
30                tf.close()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值