python安装PIL

安装依赖

1
2
3
4
sudo apt-get install libjpeg-dev
sudo apt-get install libfreetype6-dev
sudo apt-get install zlib-devel

安装 PIL

1
2
sudo pip install -U PIL --allow-external PIL --allow-unverified PIL

mac

mac 安装 PIL 的时候出现了两个错误,主要是库的问题.

错误

1
2
3
4
5
6
_imagingft.c:73:10: fatal error: 'freetype/fterrors.h' file not found
#include <freetype/fterrors.h>
^
1 error generated.
error: command 'cc' failed with exit status 1

方法

1
2
ln -s /usr/local/include/freetype2 /usr/local/include/freetype

错误

1
2
3
4
5
6
7
8
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/tk.h:78:11: fatal error: 'X11/Xlib.h' file not found

# include <X11/Xlib.h>

^
1 error generated.
error: command 'cc' failed with exit status 1

方法

1
2
3
ln -s  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers/X11 /usr/local/include/X11


ubuntu 上安装的错误

错误

1
2
3
4
5
6
7
8
9
10
11
12
imaging.c:75:20: fatal error: Python.h: No such file or directory

#include "Python.h"

^

compilation terminated.

error: command 'x86_64-linux-gnu-gcc' failed with exit status 1



方法

1
2
3
4
5
上面是 python 库的问题,可能是默认的 python 路径的问题,可以重新安装来解决

sudo apt-get install python-dev


错误

1
2
IOError: decoder zip not available

方法

有可能是 zlib 的问题

1
2
3
4
5
6
7
8
9
10
11
sudo apt-get install python-dev libjpeg-dev libfreetype6-dev zlib1g-dev

# create these links, if already exists, remove it and re-link it
ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib
ln -s /usr/lib/x86_64-linux-gnu/libfreetype.so /usr/lib
ln -s /usr/lib/x86_64-linux-gnu/libz.so /usr/lib

# reinstall PIL
pip uninstall PIL
pip install PIL

错误

1
2
python PIL bug: NoneType object has no attribute bands

方法

在使用 python PIL 获取分量的时候出现了错误

1
2
r,g,b,a = image.split()

1
2
'NoneType' object has no attribute 'bands'

原因是图片遇到 mode 为 RGBA 的时候会出现这个问题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
vim /usr/local/lib/python2.7/dist-packages/PIL/Image.py

定位到1501 行
1494 def split(self):
1495 "Split image into bands"
1496
1497 self.load() #
1498 if self.im.bands == 1:
1499 ims = [self.copy()]
1500 else:
1501 #self.load()
1502 ims = []
1503 for i in range(self.im.bands):
1504 ims.append(self._new(self.im.getband(i)))
1505 return tuple(ims)

将else中得self.load() 放到 if 外面来

参考

http://stackoverflow.com/questions/20325473/error-installing-python-image-library-using-pip-on-mac-os-x-10-9

http://stackoverflow.com/questions/19532125/cant-install-pil-after-mac-os-x-10-9

http://stackoverflow.com/questions/3544155/about-the-pil-error-ioerror-decoder-zip-not-available

作者

张巍

发布于

2015-08-04

更新于

2015-08-04

许可协议

评论