Python图像库失败,并显示消息“解码器JPEG不可用”-PIL

问题:Python图像库失败,并显示消息“解码器JPEG不可用”-PIL

PIL在我的系统中确实支持JPEG。

每当我上传时,我的代码都会失败并显示以下内容:

File "PIL/Image.py", line 375, in _getdecoder
    raise IOError("decoder %s not available" % decoder_name)
IOError: decoder jpeg not available

我该如何解决?

PIL does support JPEG in my system.

Whenever I do an upload, my code is failing with:

File "PIL/Image.py", line 375, in _getdecoder
    raise IOError("decoder %s not available" % decoder_name)
IOError: decoder jpeg not available

How can I resolve this?


回答 0

需要libjpeg-dev才能处理带有枕头(或PIL)的jpeg,因此您需要先安装它,然后重新编译枕头。在Ubuntu 14.04上似乎还需要libjpeg8-dev

如果您仍在使用PIL,那么这些天确实应该使用枕头,因此pip uninstall PIL请先遵循以下说明进行切换,或者如果您有充分的理由坚持使用PIL,请在下面将“枕头”替换为“ PIL” )。

在Ubuntu上:

# install libjpeg-dev with apt
sudo apt-get install libjpeg-dev
# if you're on Ubuntu 14.04, also install this
sudo apt-get install libjpeg8-dev

# reinstall pillow
pip install --no-cache-dir -I pillow

如果这不起作用,请根据您使用的是64位还是32位Ubuntu,尝试以下操作之一。

对于Ubuntu x64:

sudo ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib
sudo ln -s /usr/lib/x86_64-linux-gnu/libfreetype.so /usr/lib
sudo ln -s /usr/lib/x86_64-linux-gnu/libz.so /usr/lib

或对于Ubuntu 32位:

sudo ln -s /usr/lib/i386-linux-gnu/libjpeg.so /usr/lib/
sudo ln -s /usr/lib/i386-linux-gnu/libfreetype.so.6 /usr/lib/
sudo ln -s /usr/lib/i386-linux-gnu/libz.so /usr/lib/

然后重新安装枕头:

pip install --no-cache-dir -I pillow

(进行编辑以包含来自评论的反馈。感谢Charles Offenbacher指出32位版本存在差异,而t-mart建议使用--no-cache-dir)。

libjpeg-dev is required to be able to process jpegs with pillow (or PIL), so you need to install it and then recompile pillow. It also seems that libjpeg8-dev is needed on Ubuntu 14.04

If you’re still using PIL then you should really be using pillow these days though, so first pip uninstall PIL before following these instructions to switch, or if you have a good reason for sticking with PIL then replace “pillow” with “PIL” in the below).

On Ubuntu:

# install libjpeg-dev with apt
sudo apt-get install libjpeg-dev
# if you're on Ubuntu 14.04, also install this
sudo apt-get install libjpeg8-dev

# reinstall pillow
pip install --no-cache-dir -I pillow

If that doesn’t work, try one of the below, depending on whether you are on 64bit or 32bit Ubuntu.

For Ubuntu x64:

sudo ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib
sudo ln -s /usr/lib/x86_64-linux-gnu/libfreetype.so /usr/lib
sudo ln -s /usr/lib/x86_64-linux-gnu/libz.so /usr/lib

Or for Ubuntu 32bit:

sudo ln -s /usr/lib/i386-linux-gnu/libjpeg.so /usr/lib/
sudo ln -s /usr/lib/i386-linux-gnu/libfreetype.so.6 /usr/lib/
sudo ln -s /usr/lib/i386-linux-gnu/libz.so /usr/lib/

Then reinstall pillow:

pip install --no-cache-dir -I pillow

(Edits to include feedback from comments. Thanks Charles Offenbacher for pointing out this differs for 32bit, and t-mart for suggesting use of --no-cache-dir).


回答 1

对于OSX上的操作系统,我使用以下二进制文件来在系统范围内安装libpng和libjpeg:

适用于OSX的libpng和libjpeg

因为我已经安装了PIL(通过virtualenv上的pip),所以运行:

pip uninstall PIL
pip install PIL --upgrade

decoder JPEG not available为我解决了错误。

更新(4/24/14)

较新版本的pip需要附加标志才能从外部源下载库(包括PIL)。请尝试以下操作:

pip install PIL --allow-external PIL --allow-unverified PIL

有关其他信息,请参见以下答案:pip install PIL不要安装到virtualenv中

更新2

如果在OSX Mavericks上,则需要将ARCHFLAGS标志设置为@RicardoGonzales注释,如下所示

ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future pip install PIL --allow-external PIL --allow-unverified PIL

For those on OSX, I used the following binary to get libpng and libjpeg installed systemwide:

libpng & libjpeg for OSX

Because I already had PIL installed (via pip on a virtualenv), I ran:

pip uninstall PIL
pip install PIL --upgrade

This resolved the decoder JPEG not available error for me.

UPDATE (4/24/14):

Newer versions of pip require additional flags to download libraries (including PIL) from external sources. Try the following:

pip install PIL --allow-external PIL --allow-unverified PIL

See the following answer for additional info: pip install PIL dont install into virtualenv

UPDATE 2:

If on OSX Mavericks, you’ll want to set the ARCHFLAGS flag as @RicardoGonzales comments below:

ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future pip install PIL --allow-external PIL --allow-unverified PIL

回答 2

这是对我有用的唯一方法。安装软件包和重新安装PIL无效。

在ubuntu上,安装所需的软件包:

sudo apt-get install libjpeg-dev

(您可能还需要安装libfreetype6 libfreetype6-dev zlib1g-dev以启用其他解码器)。

然后用枕头更换PIL:

pip uninstall PIL
pip install pillow

This is the only way that worked for me. Installing packages and reinstalling PIL didn’t work.

On ubuntu, install the required package:

sudo apt-get install libjpeg-dev

(you may also want to install libfreetype6 libfreetype6-dev zlib1g-dev to enable other decoders).

Then replace PIL with pillow:

pip uninstall PIL
pip install pillow

回答 3

以下是在Ubuntu 12.04上的工作:

pip uninstall PIL
apt-get install libjpeg-dev
apt-get install libfreetype6-dev
apt-get install zlib1g-dev
apt-get install libpng12-dev
pip install PIL --upgrade

当您看到“-JPEG支持可用”时,表明它可以工作。

但是,如果在编辑jpeg图像时仍然不起作用,请检查python路径!我的python路径未命中/usr/local/lib/python2.7/dist-packages/PIL-1.1.7-py2.7-linux-x86_64.egg/,因此我~/.bashrc将以下代码添加到该文件中:

编辑: export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/dist-packages/PIL-1.1.7-py2.7-linux-x86_64.egg/

然后,终于可以了!!

The followed works on ubuntu 12.04:

pip uninstall PIL
apt-get install libjpeg-dev
apt-get install libfreetype6-dev
apt-get install zlib1g-dev
apt-get install libpng12-dev
pip install PIL --upgrade

when your see “– JPEG support avaliable” that means it works.

But, if it still doesn’t work when your edit your jpeg image, check the python path !! my python path missed /usr/local/lib/python2.7/dist-packages/PIL-1.1.7-py2.7-linux-x86_64.egg/, so I edit the ~/.bashrc add the following code to this file:

Edit: export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/dist-packages/PIL-1.1.7-py2.7-linux-x86_64.egg/

then, finally, it works!!


回答 4

在Fedora 17上,我必须先安装libjpeg-devel然后重新安装PIL

sudo yum install --assumeyes libjpeg-devel
sudo pip-python install --upgrade PIL

On Fedora 17 I had to install libjpeg-devel and afterwards reinstall PIL:

sudo yum install --assumeyes libjpeg-devel
sudo pip-python install --upgrade PIL

回答 5

Rolo的回答非常好,但是我不得不绕过pip缓存(pip 7引入)来重新安装Pillow,否则它将无法正确地重新编译!!!该命令是:

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

您可以通过在日志中读取以下内容来查看是否已正确配置枕头:

PIL SETUP SUMMARY
    --------------------------------------------------------------------
    version      Pillow 2.8.2
    platform     linux 3.4.3 (default, May 25 2015, 15:44:26)
                 [GCC 4.8.2]
    --------------------------------------------------------------------
    *** TKINTER support not available
    --- JPEG support available
    *** 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
    --------------------------------------------------------------------

如您所见,启用了对jpg,tiff等的支持,因为我以前通过apt(libjpeg-dev libpng12-dev libfreetype6-dev libtiff-dev)安装了所需的库。

Rolo’s answer is excellent, however I had to reinstall Pillow by bypassing pip cache (introduced with pip 7) otherwise it won’t get properly recompiled!!! The command is:

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

and you can see if Pillow has been properly configured by reading in the logs this:

PIL SETUP SUMMARY
    --------------------------------------------------------------------
    version      Pillow 2.8.2
    platform     linux 3.4.3 (default, May 25 2015, 15:44:26)
                 [GCC 4.8.2]
    --------------------------------------------------------------------
    *** TKINTER support not available
    --- JPEG support available
    *** 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
    --------------------------------------------------------------------

as you can see the support for jpg, tiff and so on is enabled, because I previously installed the required libraries via apt (libjpeg-dev libpng12-dev libfreetype6-dev libtiff-dev)


回答 6

在Mac OS X Mavericks(10.9.3)上,我通过执行以下操作解决了此问题:

通过brew安装libjpeg (软件包管理系统)

酿造安装libjpeg

重新安装枕头(我用枕头代替PIL)

点安装-我枕头

On Mac OS X Mavericks (10.9.3), I solved this by doing the follows:

Install libjpeg by brew (package management system)

brew install libjpeg

reinstall pillow (I use pillow instead of PIL)

pip install -I pillow


回答 7

apt-get install libjpeg-dev
apt-get install libfreetype6-dev
apt-get install zlib1g-dev
apt-get install libpng12-dev

安装这些文件并确保使用pip安装PIL,因为我是从源代码编译它的,由于某种原因它不起作用

apt-get install libjpeg-dev
apt-get install libfreetype6-dev
apt-get install zlib1g-dev
apt-get install libpng12-dev

Install these and be sure to install PIL with pip because I compiled it from source and for some reason it didn’t work


回答 8

我已经在使用Pillow并且遇到了同样的错误。尝试安装libjpeglibjpeg-dev按照其他人的建议进行安装,但被告知已经安装了(较新的)版本。

最后只需要重新安装Pillow

sudo pip uninstall Pillow
sudo pip install Pillow

I was already using Pillow and got the same error. Tried installing libjpeg or libjpeg-dev as suggested by others but was told that a (newer) version was already installed.

In the end all it took was reinstalling Pillow:

sudo pip uninstall Pillow
sudo pip install Pillow

回答 9

我太新手,无法评论zeantsoi post;(。因此,这里他需要做的工作才能在10.9.1上的OSX上解决

IOError:解码器jpeg不可用

1)安装Xcode工具(打开您的终端并执行: xcode-select --install)-摘自本文:Mac OS X 10.9之后无法安装PIL

2)从此链接安装libpng和libjpeg软件包(组合安装程序):http : //ethan.tira-thompson.com/Mac_OS_X_Ports.html

3)重新启动(不确定是否是强制性的)

4)使用run 重新安装PILpip install -I PIL(就像我在出现问题之前最初安装PIL一样)

希望有帮助,不要混淆更多…

_oho

I’m too newbie to comment zeantsoi post ;(. So here his what I needed to do to solved on OSX on 10.9.1 the

IOError: decoder jpeg not available

1) install Xcode tools (open your terminal and execute: xcode-select --install) – taken from this post: Can’t install PIL after Mac OS X 10.9

2) install libpng and libjpeg package (combo installer) from this link: http://ethan.tira-thompson.com/Mac_OS_X_Ports.html

3) reboot (not sure it was mandatory)

4) Re-install PIL with run pip install -I PIL (as I had initially installed PIL before having the issue)

Hope this help and don’t confuse more …

_oho


回答 10

这个问题是在很久以前发布的,而且大多数答案也很老。因此,当我花数小时试图弄清楚这一点时,没有任何效果,并且我尝试了本文中的所有建议。

尝试以Django头像形式上载JPG时,我仍然遇到标准JPEG错误:

raise IOError("decoder %s not available" % decoder_name)
OSError: decoder jpeg not available

然后,我检查了Ubuntu 12.04的存储库,发现有一些额外的软件包libjpeg。我安装了这些,问题就解决了:

sudo apt-get install libjpeg62 libjpeg62-dev

安装这些去掉libjpeg-devlibjpeg-turbo8-devlibjpeg8-dev

希望这对2015年及以后的人有所帮助!

干杯

This question was posted quite a while ago and most of the answers are quite old too. So when I spent hours trying to figure this out, nothing worked, and I tried all suggestions in this post.

I was still getting the standard JPEG errors when trying to upload a JPG in my Django avatar form:

raise IOError("decoder %s not available" % decoder_name)
OSError: decoder jpeg not available

Then I checked the repository for Ubuntu 12.04 and noticed some extra packages for libjpeg. I installed these and my problem was solved:

sudo apt-get install libjpeg62 libjpeg62-dev

Installing these removed libjpeg-dev, libjpeg-turbo8-dev, and libjpeg8-dev.

Hope this helps someone in the year 2015 and beyond!

Cheers


回答 11

同样的问题在这里,JPEG support available但是仍然得到了IOError: decoder/encoder jpeg not available,除了我使用枕头而不是PIL。

我尝试了以上所有方法,但经过多个小时,我意识到与结合使用sudo pip install不能按预期工作virtualenv。傻我

使用sudo有效在未激活virtualenv 的新外壳中启动命令(我的理解可能并不完全正确),这意味着这些软件包将安装在全局环境中。(这把事情弄糟了,我想我有2个不同的Pillow安装。)

我清理了所有内容,将用户更改为root,然后将其重新安装在virtualenv中,现在可以使用了。
希望这会帮助某人!

Same problem here, JPEG support available but still got IOError: decoder/encoder jpeg not available, except I use Pillow and not PIL.

I tried all of the above and more, but after many hours I realized that using sudo pip install does not work as I expected, in combination with virtualenv. Silly me.

Using sudo effectively launches the command in a new shell (my understanding of this may not be entirely correct) where the virtualenv is not activated, meaning that the packages will be installed in the global environment instead. (This messed things up, I think I had 2 different installations of Pillow.)

I cleaned things up, changed user to root and reinstalled in the virtualenv and now it works.
Hopefully this will help someone!


回答 12

对于Fedora

安装必备组件
sudo dnf install make automake gcc gcc-c++ kernel-devel rpm-build libjpeg-devel zlib-devel python-devel
现在安装枕头

sudo pip install pillow

注意-对于libjpeg和zlib,我们在Fedora / CentOS / Red Hat 中安装libjpeg-develzlib-devel软件包。

For Fedora

Install pre-requisite
sudo dnf install make automake gcc gcc-c++ kernel-devel rpm-build libjpeg-devel zlib-devel python-devel
Now install Pillow

sudo pip install pillow

Note – For libjpeg and zlib we are installing libjpeg-devel and zlib-devel packages in Fedora/CentOS/Red Hat


回答 13

首先,除了卸载Python之外,我还必须删除隐藏文件夹user / appData中的python文件夹(这很麻烦)。然后,我安装了WinPython发行版:http : //code.google.com/p/winpython/,其中包含PIL

First I had to delete the python folders in hidden folder user/appData (that was creating huge headaches), in addition to uninstalling Python. Then I installed WinPython Distribution: http://code.google.com/p/winpython/ which includes PIL


回答 14

对于Mac OS Mountain Lion上的用户,我遵循了zeantsoi的提示,但是它不起作用。

我最终以这篇文章的解决方案结束:http : //appelfreelance.com/2010/06/libjpeg-pil-snow-leopard-python2-6-__jpeg_resync_to_restart/

现在,我很高兴为jpeg运行脚本!

For those on Mac OS Mountain Lion, I followed the anwser of zeantsoi, but it doesn’t work.

I finally ended up with the solution of this post: http://appelfreelance.com/2010/06/libjpeg-pil-snow-leopard-python2-6-_jpeg_resync_to_restart/

Now, I’m happily running my script for jpeg !