PIL / JPEG库:"解码器jpeg不可用"

56

我尝试在我的Django应用程序中使用PIL进行一些JPEG处理,但是我遇到了这个IOError..不确定该怎么办。

""decoder jpeg not available""

我是否缺少服务器上的JPEG解码器?如果是这样,我该怎么修复它?


我发现了这篇文章,它真的帮助了我:http://obroll.com/install-python-pil-python-image-library-on-ubuntu-11-10-oneiric/ - kschaeffler
8个回答

73

我从作者"edward"那里找到了这个答案

在Ubuntu Precise版本中,即使安装了jpeg库文件,PIL也无法找到它们。最简单的解决方法是在安装了jpeg开发包后创建一个符号链接。所以,在我的情况下需要多执行一步:

对于x64操作系统

pip uninstall PIL
sudo apt-get install libjpeg8-dev
sudo ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib
pip install PIL

适用于 x32 操作系统

pip uninstall PIL
sudo apt-get install libjpeg8-dev
sudo ln -s /usr/lib/i386-linux-gnu/libjpeg.so /usr/lib
pip install PIL

我确认这在Ubuntu Linux 12.04上可行。


如果您是Mac用户,您需要安装Xcode和命令行工具。 阅读如何安装


2
sudo ln -s /usr/lib/i386-linux-gnu/libjpeg.so /usr/lib 对我来说起到了作用。 - Nathan Keller
Nathan Keller,这取决于您的系统版本。是x64还是x32。谢谢。我已经编辑了我的答案。 - Anton Danilchenko
1
我在这里找到了Mac用户的解决方案:http://python-on-mac.blogspot.com/2013/02/prepare-your-mac-to-work-in-python.html - Anton Danilchenko
2
Mac用户不需要安装所有的XCode来使用PIL编写简单的jpeg文件。只需运行brew install libjpeg,然后按照上述说明重新安装PIL即可开始工作。 - Craig Labenz

60

您需要先安装jpeg库并重新安装PIL。例如,我使用的是CentOS,在安装libjpeg之前,我运行以下命令:

sudo yum install -y libjpeg-devel

这取决于你使用的Linux版本。在此之前,你需要删除旧的PIL。

rm -rf /usr/lib/python2.6/site-packages/PIL-1.1.7-py2.6-linux-x86_64.egg/

然后安装PIL

sudo easy_install PIL

20

更为详尽的答案可以在在virtualenv中使用libjpeg安装PIL找到。

对于我在Ubuntu上的解决方法是:

pip uninstall PIL
sudo apt-get install libjpeg8-dev
pip install PIL

Python Imaging Library (PIL) 对JPEG库的版本和位置非常挑剔。因为PIL是用C语言编写并编译的,所以您需要除运行时版本外还需要开发版本的库。


在Ubuntu 12.04.2 LTS上使用Pillow库,以下命令适用: pip uninstall Pillow sudo apt-get install libjpeg8-dev pip install Pillow 这对我来说有效。 - Moreno

6

我没有sudo权限,因为我在一个共享的bluehost服务器上。所以我不能运行任何sudo apt-get jpeg命令。我最终运行了

pip uninstall pil
pip install pillow

显然,Pillow 能够支持 JPEG 格式。


2
实际上,我在执行这个操作之后仍然遇到了IOError: decoder jpeg not available的问题。 - AJP
1
以下是@cmaluenda的答案和这个答案的结合起来,可以解决问题。 - AJP
是的,但如果你在运行Mac服务器,你在解决这个问题之前会遇到更多的问题。 - Chase Roberts

4
您的Imaging安装文件夹中有一个selftest.py文件,尝试运行以下命令:
python selftest.py

您将会看到类似于这样的内容:
--------------------------------------------------------------------
*** PIL CORE support not installed
*** TKINTER support not installed
--- JPEG support ok
--- ZLIB (PNG/ZIP) support ok
*** FREETYPE2 support not installed
*** LITTLECMS support not installed
--------------------------------------------------------------------

如果JPEG支持不可用,CentOS:
yum install libjpeg-devel
yum install freetype-devel 
yum install libpng-devel

3

有没有办法使用Brew而不是MacPorts? - chovy

0

我已经尝试过了,但运行自测时仍然提示:来自selftest.testimage的第24行 期望值:('JPEG','RGB',(128, 128)) 实际值:解码器jpeg不可用 1个测试项失败: selftest.testimage中的57个测试项之一 测试失败 1个失败。 ***57个测试中有1个失败。 - Brian D
我假设整个"./configure --with-jpeg=/somelib/lib --with-zlib=/somelib/lib"命令行意味着我需要获取jpeg库的副本。 - Brian D
是的,但你可能可以通过软件包管理器来做到这一点,例如在Ubuntu上使用“apt-get install libjpeg-dev”。 - TryPyPy

0

在Debian发行版中,使用libjpeg62-turbo-dev而不是libjpeg8-dev


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接