标签归档:pillow

在Linux中安装Pillow(Python模块)期间失败

问题:在Linux中安装Pillow(Python模块)期间失败

我正在尝试使用pip安装Pillow(Python模块),但是会引发以下错误:

ValueError: jpeg is required unless explicitly disabled using --disable-jpeg, aborting

因此,正如错误所述,我尝试了:

pip install pillow --global-option="--disable-jpeg"

但是它失败了:

error: option --disable-jpeg not recognized

有什么提示如何处理吗?

I’m trying to install Pillow (Python module) using pip, but it throws this error:

ValueError: jpeg is required unless explicitly disabled using --disable-jpeg, aborting

So as the error says, I tried:

pip install pillow --global-option="--disable-jpeg"

But it fails with:

error: option --disable-jpeg not recognized

Any hints how to deal with it?


回答 0

有报道枕头的错误在这里,这表明libjpegzlib现在需要的枕头3.0.0。

Linux上Pillow 的安装说明提供了有关如何安装这些软件包的建议。请注意,并非以下所有软件包都可能在您的计算机上丢失(注释表明libjpeg8-dev实际上仅缺少这些软件包)。

点/ PyPi(枕头> 3.4.2)

最新版本枕头的可PyPI上的轮子 – Python的新标准包装机制。这些预构建的软件包包括所有必要的二进制依赖关系,以允许Pillow运行,如果要使用PyPi安装Pillow,则应使用这些预构建包。

要使用轮子,您需要具有的版本pip>=1.4。如果您使用的是早期版本(pip --version),请使用以下方法升级pip:

pip install --upgrade pip 

一旦pip升级,pip install将在默认情况下,如果他们都可以使用特定于平台的轮文件。使用以下命令将Pillow升级到PyPi上可用的最新版本:

pip install --upgrade pillow

Ubuntu 12.04 LTS或Raspian Wheezy 7.0

sudo apt-get install libtiff4-dev libjpeg8-dev zlib1g-dev libfreetype6-dev liblcms2-dev libwebp-dev tcl8.5-dev tk8.5-dev python-tk

Ubuntu 14.04

sudo apt-get install libtiff5-dev libjpeg8-dev zlib1g-dev libfreetype6-dev liblcms2-dev libwebp-dev tcl8.6-dev tk8.6-dev python-tk

Ubuntu 18.04

sudo apt install libjpeg8-dev zlib1g-dev

软呢帽20

的Fedora 20当量的libjpeg8-devlibjpeg-devel

sudo yum install libtiff-devel libjpeg-devel libzip-devel freetype-devel lcms2-devel libwebp-devel tcl-devel tk-devel

Mac OS X(通过Homebrew)

在带有Homebrew的 Mac OS X上,可以使用以下方法解决此问题:

brew install libjpeg zlib

您可能还需要使用以下命令强制链接zlib:

brew link zlib --force

20194月更新:在Mojave中,以上操作不起作用,您需要运行此错误报告(取自Pillow上的此错误报告)

sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

2016年7月更新:zlib主存储库中不再有可用的公式(Homebrew将提示您安装lzlib哪个是其他库,将无法解决此问题)。

还有就是在可用公式受骗者库。您可以点击此存储库,然后按常规安装:

brew tap homebrew/dupes
brew install zlib

或者,您也可以改为zlib通过安装xcode,如下所示:

xcode-select --install

感谢凤凰城,Panos Angelopoulou,nelsonvarela,benjaminz和Kal在评论中

这些安装后,枕头的点子安装应该可以正常工作。

There is a bug reported for Pillow here, which indicates that libjpeg and zlib are now required as of Pillow 3.0.0.

The installation instructions for Pillow on Linux give advice of how to install these packages. Note that not all of the following packages may be missing on your machine (comments suggest that only libjpeg8-dev is actually missing).

pip / PyPi (Pillow>3.4.2)

The latest releases of Pillow are available on PyPi as wheels — the new standard packaging mechanism for Python. These prebuilt packages include all neccessary binary dependencies to allow Pillow to run and should be used if you want to install Pillow using PyPi

To use wheels, you need to have a version of pip>=1.4. If you are using an earlier version (pip --version) upgrade pip using the following:

pip install --upgrade pip 

Once pip is upgraded, pip install will use platform-specific wheel files by default if they are available. Use the following command to upgrade Pillow to the latest version available on PyPi:

pip install --upgrade pillow

Ubuntu 12.04 LTS or Raspian Wheezy 7.0

sudo apt-get install libtiff4-dev libjpeg8-dev zlib1g-dev libfreetype6-dev liblcms2-dev libwebp-dev tcl8.5-dev tk8.5-dev python-tk

Ubuntu 14.04

sudo apt-get install libtiff5-dev libjpeg8-dev zlib1g-dev libfreetype6-dev liblcms2-dev libwebp-dev tcl8.6-dev tk8.6-dev python-tk

Ubuntu 18.04

sudo apt install libjpeg8-dev zlib1g-dev

Fedora 20

The Fedora 20 equivalent of libjpeg8-dev is libjpeg-devel.

sudo yum install libtiff-devel libjpeg-devel libzip-devel freetype-devel lcms2-devel libwebp-devel tcl-devel tk-devel

Mac OS X (via Homebrew)

On Mac OS X with Homebrew this can be fixed using:

brew install libjpeg zlib

You may also need to force-link zlib using the following:

brew link zlib --force

Update April 2019: In Mojave the above will not work and you need to run the following as taken from this bug report on Pillow

sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

Update July 2016: There is no longer a formula for zlib available in the main repository (Homebrew will prompt you to install lzlib which is a different library and will not solve this problem).

There is a formula available in the dupes repository. You can either tap this repository, and install as normal:

brew tap homebrew/dupes
brew install zlib

Or you can install zlib via xcode instead, as follows:

xcode-select --install

Thanks to phoenix, Panos Angelopoulou, nelsonvarela, benjaminz and Kal in the comments

After these are installed the pip installation of Pillow should work normally.


回答 1

在Raspberry pi II上,我遇到了同样的问题。尝试以下方法后,我解决了问题。解决方案是:

sudo apt-get update
sudo apt-get install libjpeg-dev

On Raspberry pi II, I had the same problem. After trying the following, I solved the problem. The solution is:

sudo apt-get update
sudo apt-get install libjpeg-dev

回答 2

谢谢@mfitzp。就我而言(CentOS),这些库在yum存储库中不可用,但实际上解决方案甚至更容易。我做了什么:

sudo yum install python-devel
sudo yum install zlib-devel
sudo yum install libjpeg-turbo-devel

至此,枕头的安装成功完成。

Thank you @mfitzp. In my case (CentOS) these libs are not available in the yum repo, but actually the solution was even easier. What I did:

sudo yum install python-devel
sudo yum install zlib-devel
sudo yum install libjpeg-turbo-devel

And now pillow’s installation finishes successfully.


回答 3

最快的解决方法是提高点子。确实为我工作:

pip install --upgrade pip

The quickest fix is upgrate the pip. Did worked for me:

pip install --upgrade pip

回答 4

这对我有用。

   `sudo apt-get install libjpeg-dev`

This worked for me.

   `sudo apt-get install libjpeg-dev`

回答 5

brew install zlib

在OS X上不再工作,而是提示安装lzlib。安装没有帮助。

相反,您需要安装XCode命令行工具,并且应该安装 zlib

xcode-select --install
brew install zlib

on OS X doesn’t work anymore and instead prompts to install lzlib. Installing that doesn’t help.

Instead you install XCode Command line tools and that should install zlib

xcode-select --install

回答 6

这对我来说解决了jpeg和zlib错误:

C:\Windows\system32>pip3 install pillow --global-option="build_e
xt" --global-option="--disable-zlib" --global-option="--disable-jpeg"

This worked for me to solve jpeg and zlib error :

C:\Windows\system32>pip3 install pillow --global-option="build_e
xt" --global-option="--disable-zlib" --global-option="--disable-jpeg"

回答 7

ValueError: zlib is required unless explicitly disabled using --disable-zlib将点子从7.x升级到8.y可以解决问题。

所以我会先尝试更新工具。

可以使用以下方法完成:

pip install --upgrade pip

I had the ValueError: zlib is required unless explicitly disabled using --disable-zlib but upgrading pip from 7.x to 8.y resolved the problem.

So I would try to update tools before anything else.

That can be done using:

pip install --upgrade pip

回答 8

如果您不想安装libjpeg,也可以选择:

CFLAGS="--disable-jpeg" pip install pillow

来自https://pillow.readthedocs.io/zh/3.0.0/installation.html#external-libraries

The alternative, if you don’t want to install libjpeg:

CFLAGS="--disable-jpeg" pip install pillow

From https://pillow.readthedocs.io/en/3.0.0/installation.html#external-libraries


回答 9

尝试

pip install pillow

如果不起作用,请尝试清除

cache by pip install --upgrade pip

然后再次运行

pip install pillow

Try

pip install pillow

If it doesn’t work, try clearing the

cache by pip install --upgrade pip

Then again run

pip install pillow

回答 10

在debian / ubuntu上,您只需要:libjpeg62-turbo-dev

所以一个简单sudo apt install libjpeg62-turbo-dev 而又pip install pillow

On debian / ubuntu you only need: libjpeg62-turbo-dev

So a simple sudo apt install libjpeg62-turbo-dev and a pip install pillow


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 !


使用python识别图片每一个像素的RGB颜色

休息了一个月,从今天开始继续更新文章!希望大家喜欢。最近在研究怎么破解验证码,其中就要利用到Python识别像素颜色的技术。你也可以用PS慢慢地进行取样记录,但那样效率非常低下,而且麻烦。识别验证码需要我们高效地提取出像素,并将其转换成黑白二色,这样才好进行矢量对比。

工具

pillow

如果你有PyCharm的话,可以在Preferences内的Project Interpreter中安装pillow.

若你没有PyCharm.请上网查阅安装流程,非常简单。

几个要用到的比较重要的函数(你可以先跳过,遇到不认识的函数再回来查看):

建议直接阅读PIL的英文文档:http://effbot.org/imagingbook/image.htm

image.new(mode,size,color)

使用给定的变量mode和size生成新的图像。mode是图片模式,如”RGB”、”P”。Size是给定的宽/高二元组,这是按照像素数来计算的。对于单通道图像,变量color只给定一个值;对于多通道图像,变量color给定一个元组(每个通道对应一个值)。

im.convert(mode)

将你打开的图片转化为某种格式。

im.getpixel(xy)

返回给定位置的像素。

im.putpixel(xy,color)

修改给定位置的像素。

im.size()

返回两个元素,宽和高。im.size ⇒ (width, height)

若是变量名.size[0]则便是是宽,size[1]则表示是高。

使用的图片:

from PIL import Image

im = Image.open("1.jpg")

for y in range(im.size[1]):
    for x in range(im.size[0]):
        pix = im.getpixel((x,y))
        print(pix)

运行结果很长,我们截取一部分:

不得不说Python实在很强大。不过这样看实在很麻烦,因为像素这么多,我们不可能手动地去分析数据。

如果我们要算出RGB各个位低于100的数量,我们可以这样做:

from PIL import Image
 im = Image.open("1.jpg")
 count = 0
 for y in range(im.size[1]):
     for x in range(im.size[0]):
         pix = im.getpixel((x,y))
         if(pix[0] < 100 and pix[1] < 100 and pix[2] < 100):
             count = count + 1
 print(count)

结果是265743.我们甚至可以把它们转换成别的颜色。

最好是不在原图上操作,我们new一个一样大小的图片即可。

im2 = Image.new(“RGB”,im.size,255)

意思是新建一个跟它一样大的RGB图片,背景颜色为红色。

我们把刚刚各个位低于100的像素转化为白色。

from PIL import Image
im = Image.open("1.jpg")
im2 = Image.new("RGB",im.size,255)
count = 0
for y in range(im.size[1]):
    for x in range(im.size[0]):
        pix = im.getpixel((x,y))
        if(pix[0] < 100 and pix[1] < 100 and pix[2] < 100):
            im2.putpixel((x,y),(255,255,255))
im2.show()

结果:

如果我们对原图进行修改颜色:

嘛,还是可以接受的。

源代码已经上传到公众号的github项目。以后公众号的源代码及图片都能在这里找到:https://github.com/Ckend/GongZhongHao

欢迎关注微信公众号:Python实用宝典https://pythondict.com