pillow无法调用libjpeg的问题

问题

#!/usr/bin/python
from PIL import Image
im = Image.open('test.jpg')
print(im.format, im.size, im.mode)
im.resize((128, 128))
out = im.rotate(45)

执行后的结果如下:

(movies)[vagrant@crawler tutorial]$ python test.py
('JPEG', (1920, 1200), 'RGB')
Traceback (most recent call last):
File "test.py", line 7, in <module>
im.resize((128, 128))
File "/home/vagrant/dev/movies/lib/python2.7/site-packages/PIL/Image.py", line 1557, in resize
self.load()
File "/home/vagrant/dev/movies/lib/python2.7/site-packages/PIL/ImageFile.py", line 203, in load
d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
File "/home/vagrant/dev/movies/lib/python2.7/site-packages/PIL/Image.py", line 420, in _getdecoder
raise IOError("decoder %s not available" % decoder_name)
IOError: decoder jpeg not available

解决办法

sudo yum install --assumeyes libjpeg-devel
sudo pip install --upgrade Pillow

还是没有效果,没办法只能下载源码包重新编译安装好了
源码包地址:https://pypi.python.org/pypi/Pillow/2.9.0
解压后开始编译

(movies)[vagrant@crawler Pillow-2.9.0]$ python setup.py install
Single threaded build, not installing mp_compile: 1 processes
running install
running bdist_egg
running egg_info
writing Pillow.egg-info/PKG-INFO
writing top-level names to Pillow.egg-info/top_level.txt

注意观察编译中的概要信息

PIL SETUP SUMMARY
--------------------------------------------------------------------
version Pillow 2.9.0
platform linux2 2.7.10 (default, Aug 18 2015, 06:36:20)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-16)]
--------------------------------------------------------------------
*** TKINTER support not available
--- JPEG support available【JPEG支持了】
*** OPENJPEG (JPEG2000) support not available
--- ZLIB (PNG/ZIP) support available
--- LIBTIFF support available
--- FREETYPE2 support available
*** LITTLECMS2 support not available
*** WEBP support not available
*** WEBPMUX support not available
--------------------------------------------------------------------

至此就不会出现IOError: decoder jpeg not available的错误信息了,虽然可以正常使用了但是还有个挺疑惑的问题是pip install pillow的时候为什么没有正常的编译,而需要进入源码包里面手工执行编译命令。

巨人的肩膀

Python Image Library fails with message “decoder JPEG not available” - PIL
PIL not recognizing libjpeg support
Is libjpeg available?