某项需求需要对Linux下的jpeg图片处理,压缩保存等。需要用到Image模块,而该模块在PIL包中。
环境:CentOS release 5.5 (Final)+Python 2.7.3
1. JPEG库
1.1 确认系统安装有libjpeg库,可能在/usr/local/lib目录下,也有可能在/usr/local/jpeg/lib目录下
cd /usr/local/lib
[adwiser@host8135 lib]$ ll libjpeg.*
-rw-r--r-- 1 root root 1722066 Jul 25 19:41 libjpeg.a
-rwxr-xr-x 1 root root 919 Jul 25 19:41 libjpeg.la
lrwxrwxrwx 1 root root 16 Jul 25 19:41 libjpeg.so -> libjpeg.so.7.0.0
lrwxrwxrwx 1 root root 16 Jul 25 19:41 libjpeg.so.7 -> libjpeg.so.7.0.0
-rwxr-xr-x 1 root root 943728 Jul 25 19:41 libjpeg.so.7.0.0
[adwiser@host8135 lib]$
1.2 若不存在,则安装,否则忽略此步骤。安装libjpeg库文件
1.2.1 下载地址:
http://www.ijg.org/files/jpegsrc.v7.tar.gz
1.2.2 安装步骤
tar zvxf jpegsrc.v7.tar.gz
cd jpeg-7
./configure -prefix=/usr/local/jpeg --enable-shared --enable-static
make
sudo make install
1.2.3 检查安装结果,应该如1.1所示
2. 确认PIL库是否安装,否则安装PIL库
2.1 检查PIL库的路径,报错则需要安装,或者进入python,import Image
ll /usr/local/lib/python2.7/site-packages/PIL
ll /usr/local/lib/python2.7/site-packages/PIL.pth
2.2 安装步骤
cd Imaging-1.1.7
python setup.py build_ext -i
如果报错:
/usr/bin/ld: /usr/local/lib/libz.a(deflate.o): relocation R_X86_64_32S against `a local symbol' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libz.a: could not read symbols: Bad value
则需要重新安装,转到3,否则继续
修改setup.py文件的两个参数
ZLIB_ROOT = ("/usr/lib64/", "/usr/include")
FREETYPE_ROOT = ("/usr/lib64/","/usr/include/freetype2/freetype")
sudo python setup.py install
3 安装zlib库
3.1 下
载地址:http://down1.chinaunix.net/distfiles/zlib-1.2.3.tar.bz2
3.2 安装
bunzip2 zlib-1.2.3.tar.bz2
tar xvf zlib-1.2.3.tar
./configure
修改Makefile文件的CFLAGS 为CFLAGS=-O3 -DUSE_MMAP -fPIC
make
sudo make install
3.3 再继续安装PIL库,如2所示,出现下图所示则表示安装Ok
如果FREEETYPE2显示不可用,则需要重新安装FREETYPE2库
4 安装FREEETYPE2库
4.2 安装步骤与一般安装相同
5 验证,需要可以成功导如Image和_imaging库
6 执行相应程序,Ok
7 虽然5验证通过,但是仍然报错
decoder jpeg not available
7.1 删除已经安装的PIL库
sudo rm -rf /usr/local/lib/python2.7/site-packages/PIL
sudo rm -rf /usr/local/lib/python2.7/site-packages/PIL.pth
7.2 重新安装jpeg库,zlib库freetype2库
sudo yum install libjpeg libjpeg-devel zlib zlib-devel freetype freetype-devel lcms lcms-devel
7.3 转到2,重新安装PIL库,即可完美解决
decoder jpeg not available
7.1 删除已经安装的PIL库
sudo rm -rf /usr/local/lib/python2.7/site-packages/PIL
sudo rm -rf /usr/local/lib/python2.7/site-packages/PIL.pth
7.2 重新安装jpeg库,zlib库freetype2库
sudo yum install libjpeg libjpeg-devel zlib zlib-devel freetype freetype-devel lcms lcms-devel
7.3 转到2,重新安装PIL库,即可完美解决