如何在 PyPy 中使用 PIL?

9 投票
2 回答
10759 浏览
提问于 2025-04-16 11:01

我查了一下,但没找到关于如何在PyPy中使用PIL的教程。根据PyPy的博客,PIL是被支持的。

  • 我在我的PYTHONPATH中用pip安装了PIL。
  • 下载后,pip生成了两个.pyd文件:_imaging.pyd和_imagingmath.pyd。
  • 安装完成后,我把%PYTHONPATH%/lib/site-packages/PIL复制到了我的PyPy的site-packages目录下。
  • 当我运行我的脚本(使用了PIL)时,它提示无法导入_imaging这个C模块。

我该怎么做呢?

补充说明:我在Windows 7 x64上运行这个(python 2.7.1 32位)。

这是错误追踪信息(pypy 1.4.1 Windows版本):

Traceback (most recent call last):
  File "app_main.py", line 53, in run_toplevel
  File "tools\python\gen_images.py", line 52, in <module>
    main()
  File "tools\python\gen_images.py", line 44, in main
    image = Image.open(file)
  File "d:\pypy\site-packages\PIL\Image.py", line 1965, in open
    return factory(fp, filename)
  File "d:\pypy\site-packages\PIL\ImageFile.py", line 91, in __init__
    self._open()
  File "d:\pypy\site-packages\PIL\GifImagePlugin.py", line 97, in _open
    self.seek(0) # get ready to read first frame
  File "d:\pypy\site-packages\PIL\GifImagePlugin.py", line 152, in seek
    self.dispose = Image.core.fill("P", self.size,
  File "d:\pypy\site-packages\PIL\Image.py", line 37, in __getattr__
    raise ImportError("The _imaging C module is not installed")
ImportError: The _imaging C module is not installed

2 个回答

3

我之前没有安装 easy_install 或 pip,所以我按照 pip 的说明文档 上的步骤进行了操作:

wget https://bootstrap.pypa.io/get-pip.py
pypy get-pip.py
pypy -m pip install pillow
14

我做了这个:

$ /opt/pypy-1.4.1/bin/virtualenv test
$ cd test
$ bin/pip install PIL
...
--------------------------------------------------------------------
PIL 1.1.7 SETUP SUMMARY
--------------------------------------------------------------------
version       1.1.7
platform      linux2 2.5.2 (e503e483e9ac, Dec 21 2010, 12:02:29)
              [PyPy 1.4.1]
--------------------------------------------------------------------
*** TKINTER support not available
--- JPEG support available
--- ZLIB (PNG/ZIP) support available
--- FREETYPE2 support available
*** LITTLECMS support not available
--------------------------------------------------------------------
... 
$ bin/pypy
Python 2.5.2 (e503e483e9ac, Dec 21 2010, 12:02:29)
[PyPy 1.4.1] on linux2
>>>> import Image
>>>> im = Image.open('/path/to/file.jpg')
>>>> outfile = open('/path/to/file.png', 'wb')
>>>> im.save(outfile, 'png')

效果非常好。所以你也可以试试这个。 :)

撰写回答