PIL、Pillow安装使用方法

PIL(Python Imaging Library)是Python常用的图像处理库,而Pillow是PIL的一个友好Fork,提供了了广泛的文件格式支持,强大的图像处理能力,主要包括图像储存、图像显示、格式转换以及基本的图像处理操作等。

Pillow的文档:http://pillow.readthedocs.io/en/latest/

Pillow的github:https://github.com/python-pillow/Pillow

-----------------------------------------------------------------

0x0. Pillow的注意事项

Pillow and PIL cannot co-exist in the same environment. Before installing Pillow, please uninstall PIL.
Pillow >= 1.0 no longer supports “import Image”. Please use “from PIL import Image” instead.
Pillow >= 2.1.0 no longer supports “import _imaging”. Please use “from PIL.Image import core as _imaging” instead.

Pillow < 2.0.0 supports Python versions 2.4, 2.5, 2.6, 2.7.
Pillow >= 2.0.0 < 4.0.0 supports Python versions 2.6, 2.7, 3.2, 3.3, 3.4, 3.5
Pillow >= 4.0.0 supports Python versions 2.7, 3.3, 3.4, 3.5, 3.6

 

0x1. windows安装PIL

PIL安装很麻烦,推荐下载exe直接安装

PIL官网:http://pythonware.com/products/pil/

但现在里面下载链接访问不了,我把32位和64位版本上传到博客园以供下载。

PILwin32:https://files.cnblogs.com/files/pcat/PILwin32.zip

PILwin64:https://files.cnblogs.com/files/pcat/PILwin64.zip

 

0x2. windows安装Pillow

pip install Pillow

或者在http://www.lfd.uci.edu/~gohlke/pythonlibs/#pillow 下载wheel文件使用pip安装

 

0x3. linux安装python2.7、pip

sudo apt-get install python
sudo apt-get install python-pip

 

0x4. linux安装Pillow

在linux里安装PIL不怎么建议,依赖也多,而且会有各种报错。

kali的python默认已安装了Pillow。

在debian/ubuntu里安装Pillow:

sudo apt-get install python-imaging

或者

sudo pip install -I --no-cache-dir -v Pillow

python-imaging安装后的支持import Image

 

0x5. Pillow wrapper for PIL compatibility

github地址:https://github.com/yinpeng/pillow-pil

其作者是这样介绍:他项目中也使用Pillow替代了PIL,不过PIL和Pillow的import语句不兼容导致本项目代码修改倒还好说,问题是后来使用的依赖PIL的第三方组件不兼容,我们既不想同时安装Pillow和PIL,又不想修改第三方组件的源代码,所以他写了个很简单的Pillow-PIL,使得依赖PIL的第三方组件不用修改源代码就可以使用Pillow了。安装法子如下:

pip install Pillow-PIL

 

0x6. PIL一些报错

ImportError:The _imagingft C module is not installed

这个是在windows里安装PIL所报错,建议直接exe安装。

PIL.Image.open() IOError: cannot identify image file

首先检查图片是不是正确的,如果各种检查过还是有问题建议重装PIL

 

0x7. Pillow报KeyError问题

python
>>> import Image
>>> Image.init()
1
>>> Image.SAVE.keys()
['PCX', 'HDF5', 'EPS', 'MSP', 'XBM', 'WEBP', 'BUFR', 'PPM', 'BMP', 'TGA', 'ICO', 'TIFF', 'JPEG', 'SPIDER', 'GIF', 'GRIB', 'IM', 'FITS', 'PDF', 'WMF', 'MPO', 'PALM', 'JPEG2000', 'PNG']

如果Image.SAVE.keys()输出为空,则说明没有找到save format key,在生成图片时就会报KeyError, 此时就需要把import Image改成from PIL import Image

 

posted @ 2017-04-30 18:33  pcat  阅读(119925)  评论(2编辑  收藏  举报