在Ubuntu 12.04上安装PIL库,支持Python 2.7和Python 3.2。

14
我正在使用Ubuntu 12.04服务器,默认情况下有两个版本的Python:2.7和3.2。当我使用终端命令python setup.py install安装PIL时,PIL将与Python 2.7一起安装。如何使用Python 3.2安装PIL?因为我正在使用Python 3编写我的脚本。如果我的Eclipse使用Python 2.7,则可以成功导入PIL。但是,如果我选择Python 3作为语法语言,PIL无法导入。
附注:我已尝试以下内容: 更新: 使用python3命令会导致错误消息。
示例1:
root@sys:~/Downloads/Pillow-1.7.8# python3.2 setup.py install
Traceback (most recent call last):
  File "setup.py", line 10, in <module>
    from setuptools import Extension, setup, find_packages
ImportError: No module named setuptools

例子2:
root@sys:~/Downloads/Pillow-1.7.8# python3 setup.py install
Traceback (most recent call last):
  File "setup.py", line 10, in <module>
    from setuptools import Extension, setup, find_packages
ImportError: No module named setuptools

更新2

在运行此终端命令后

sudo apt-get install python3-setuptools

我可以已经构建了setup.py,但是最后出现了错误消息。
root@sys:~/Downloads/Pillow-1.7.8# python3 setup.py build
running build
running build_py
running build_ext
Traceback (most recent call last):
  File "setup.py", line 469, in <module>
    scripts=glob.glob("Scripts/pil*.py"),
  File "/usr/lib/python3.2/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/usr/lib/python3.2/distutils/dist.py", line 917, in run_commands
    self.run_command(cmd)
  File "/usr/lib/python3.2/distutils/dist.py", line 936, in run_command
    cmd_obj.run()
  File "/usr/lib/python3.2/distutils/command/build.py", line 126, in run
    self.run_command(cmd_name)
  File "/usr/lib/python3.2/distutils/cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "/usr/lib/python3.2/distutils/dist.py", line 936, in run_command
    cmd_obj.run()
  File "/usr/lib/python3.2/distutils/command/build_ext.py", line 344, in run
    self.build_extensions()
  File "setup.py", line 127, in build_extensions
    self.add_multiarch_paths()
  File "setup.py", line 439, in add_multiarch_paths
    '/usr/lib/' + multiarch_path_component)
TypeError: Can't convert 'bytes' object to str implicitly

更新3

使用https://github.com/python-imaging/Pillow文件时出现错误信息。

root@sys:~/Pillow-master# dir
COPYING              Images          map.c       selftest.py
decode.c             _imaging.c      outline.c   setup.py
display.c            _imagingcms.c   path.c      test
docs                 _imagingft.c    PIL         Tests
encode.c             _imagingmath.c  py3.h       Tk
github-comments.txt  _imagingtk.c    README.rst  tox.ini
github-issues.txt    libImaging      Sane
github-watchers.txt  MANIFEST.in     Scripts
root@sys:~/Pillow-master# python3 setup.py build
running build
running build_py
creating build
creating build/lib.linux-x86_64-3.2
creating build/lib.linux-x86_64-3.2/PIL
copying PIL/IcnsImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/PpmImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ContainerIO.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ImageMath.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/SunImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/BufrStubImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/GimpGradientFile.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ImageGL.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/JpegImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ImageDraw.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/PngImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/TiffTags.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/PcfFontFile.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ImagePalette.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/IcoImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/PSDraw.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/CurImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ImageEnhance.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ImageDraw2.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/Image.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/FpxImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ImageWin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/WalImageFile.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ImageGrab.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/PcdImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/OleFileIO.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/SgiImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ImageFilter.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/FitsStubImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/XbmImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/GifImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ImageStat.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ImageFileIO.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ImageChops.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/McIdasImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/EpsImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ImageOps.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/__init__.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/Hdf5StubImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/MpegImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/PaletteFile.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/TarIO.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/FontFile.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/GimpPaletteFile.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ArgImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/BmpImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/PalmImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/GribStubImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/BdfFontFile.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ExifTags.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ImageShow.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/DcxImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/MspImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/GdImageFile.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/XVThumbImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/GbrImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ImageSequence.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/PcxImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ImageTransform.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/XpmImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/MicImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ImageFile.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ImagePath.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/_binary.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/TgaImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ImageTk.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ImageCms.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ImageMode.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/WmfImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/PsdImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/IptcImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ImImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ImageFont.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/PixarImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/TiffImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/FliImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ImtImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ImageQt.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/PdfImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/ImageColor.py -> build/lib.linux-x86_64-3.2/PIL
copying PIL/SpiderImagePlugin.py -> build/lib.linux-x86_64-3.2/PIL
running build_ext
building '_imaging' extension
creating build/temp.linux-x86_64-3.2/libImaging
gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -Werror=format-security -fPIC -DHAVE_LIBJPEG -DHAVE_LIBZ -I/usr/include/freetype2 -IlibImaging -I/usr/include -I/usr/local/include -I/usr/include/python3.2mu -I/usr/include/x86_64-linux-gnu -c _imaging.c -o build/temp.linux-x86_64-3.2/_imaging.o
_imaging.c:75:20: fatal error: Python.h: No such file or directory
compilation terminated.
error: command 'gcc' failed with exit status 1

已解决

按照步骤安装

sudo aptitude install python3-setuptools
sudo easy_install3 pip
sudo apt-get install python3-dev
# download Pillow's master.zip
wget https://github.com/python-imaging/Pillow/archive/master.zip
sudo unzip master.zip
python3 setup.py build
python3 setup.py install

完成!


1
相关问题 - https://dev59.com/9Wgv5IYBdhLWcg3wNuJ5 - akaIDIOT
@wim那么包名是什么? sudo pip-3.2 install <package> - John Walker
哦,我看错了;PIL 是重点。我已经成功地让 Pillow 的当前 github 主分支在 Python3 上构建和运行。在克隆存储库后,python3 setup.py build 后跟 python3 setup.py install 应该可以工作,我想。当然,您应该安装 setuptools 的 python3 版本,因为错误指出了这一点(可能是 python3-distutils,我现在不在 ubuntu 机器上)。 - akaIDIOT
@akaIDIOT看不到上面的代码,python3 setup.py build无法工作,错误信息已经显示在上面了。 - John Walker
1
对于其他人,在构建之前,根据需要安装Tcl/Tk和jpeg、zlib(png/zip)、freetype2和littlecms的最新开发版本及库。此外,在构建和安装步骤之间确保运行python3 selftest.py以确保一切正常工作。此外,所有命令都需要sudo权限,除了wget和selftest.py。对我来说,由于在构建后添加了一些库,因此第一次自检失败,因此我不得不运行sudo python setup.py cleansudo python setup.py build_ext -i以解决问题。来源:https://pypi.python.org/pypi/Pillow/#pillow - Amndeep7
显示剩余2条评论
3个回答

2

我在评论中搞糊涂了,非常抱歉,下面是更好的版本:

错误明确说明你缺少 python3 的 setuptools。包 python3-setuptools 应该能够解决这个问题(运行 sudo apt-get install python3-setuptools)。

然后,正如你所注意到的,缺省的 PIL 不适用于 python3。然而,Pillow 的 github 主分支的当前状态 却适用于 python3。安装方法为使用 python3 setup.py buildsudo python3 setup.py install


请注意,根据 Github 的记录,版本 1.7.8 已经发布了 4 个月。你可以尝试使用当前的主分支(对我来说有效)吗? - akaIDIOT
缺少 Python.h,请安装 Python 开发库;类似于 sudo apt-get install python3-dev(你可能需要谷歌一下)。 - akaIDIOT

1

你的指示过长且没有完全脚本化,以下更好(同一平台-最近的Debian/Ubuntu):

sudo aptitude install python3-pip git
pip-3.2 install --user git+https://github.com/python-imaging/Pillow

0

对于想要在Python3中使用PIL(Pillow)与Scipy一起工作的人

按照Chin Ye的步骤进行操作后,只需修改文件中的两行代码即可让import scipy.misc命令可用,并使得scipy.misc.imread和scipy.misc.imsave可用。

/usr/lib/python3/dist-packages/scipy/misc/pilutil.py

在那个文件中,替换这些行:

import Image
import ImageFilter

变成:

from PIL import Image
from PIL import ImageFilter

scipy.misc.__init__中的导入语句然后可以让pilutil.py中的内容工作(尤其是图像输入输出 - 来自numpy数组格式)

这是针对Ubuntu 12.04 LTS,scipy.__version__ 0.9.0

(有关调整本身的更多信息,请参见PIL的Pillow实现文档)


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