JPEG和PNG库在PIL中使用

时间:2012-06-08 13:57:52

标签: python-imaging-library

我对开发相对较新,我一直在尝试设置我在Django(在Windows 7,64位上)构建的应用程序,以通过ImageField接受图像上传。我最初安装了PIL,但发现我首先需要JPEG和PNG库。所以我使用“pip uninstall pil”卸载了PIL并在C:\ zlib-1.2.7 \ zlib.lib和C:\ jpeg-8d \ libjpeg.lib中设置了库。之后,我进入了PIL中的setup.py并更改了以下内容:

    JPEG_ROOT = "C:/jpeg-8d"
    ZLIB_ROOT = "C:/zlib-1.2.7"

然后我通过以下方式安装:

    pip install C:\Imaging-1.1.7\

我在安装结束时得到以下内容,这表明有JPEG和PNG支持:

Installing collected packages: PIL
Running setup.py install for PIL
WARNING: '' not a valid package name; please use only.-separated package nam
es in setup.py
--------------------------------------------------------------------
PIL 1.1.7 SETUP SUMMARY
--------------------------------------------------------------------
version       1.1.7
platform      win32 2.6.6 (r266:84297, Aug 24 2010, 18:46:32)
              [MSC v.1500 32 bit (Intel)]
--------------------------------------------------------------------
*** TKINTER support not available (Tcl/Tk 8.5 libraries needed)
--- JPEG support available
--- ZLIB (PNG/ZIP) support available
*** FREETYPE2 support not available
*** LITTLECMS support not available
--------------------------------------------------------------------
To add a missing option, make sure you have the required
library, and set the corresponding ROOT variable in the
setup.py script.

To check the build, run the selftest.py script.
Successfully installed PIL
Cleaning up...

但是,我在使用selftest.py进行测试时得到了关注,这表明没有支持:

C:\Windows\system32>python C:\Imaging-1.1.7\selftest.py
--------------------------------------------------------------------
PIL 1.1.7 TEST SUMMARY
--------------------------------------------------------------------
Python modules loaded from C:\Users\ayan\Desktop\Imaging-1.1.7\PIL
Binary modules loaded from C:\Python26_x86\lib\site-packages\PIL
--------------------------------------------------------------------
*** PIL CORE support not installed
*** TKINTER support not installed
*** JPEG support not installed
*** ZLIB (PNG/ZIP) support not installed
*** FREETYPE2 support not installed
*** LITTLECMS support not installed
--------------------------------------------------------------------

我还尝试使用JPEG并得到以下IOError:

C:\Users\Public\Pictures\Sample Pictures>python
Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import PIL
>>> import os, sys
>>> import Image
>>> img = Image.open(Desert.jpg)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'Desert' is not defined
>>> img = Image.open("Desert.jpg")
>>> img.save("Desert_test.jpg")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python26_x86\lib\site-packages\PIL\Image.py", line 1406, in save
    self.load()
  File "C:\Python26_x86\lib\site-packages\PIL\ImageFile.py", line 189, in load
    d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
  File "C:\Python26_x86\lib\site-packages\PIL\Image.py", line 385, in _getdecode
r
    raise IOError("decoder %s not available" % decoder_name)
IOError: decoder jpeg not available

这与https://stackoverflow.com/questions/10543581/pil-installation-run-issue报道的内容有些相似;然而,在这种情况下,似乎JPEG实际上不起作用。 PNG也出现了类似的问题。

我不清楚在这个过程中我犯了什么错误,所以任何评论都会非常感激。如果需要更多信息,请告诉我,我会尽力提供。

非常感谢。

1 个答案:

答案 0 :(得分:0)

我已经解决了类似的问题,但只是为了支持png。我相信你也可以为jpeg支持做同样的事情。

首先,我建议使用Pillow的Pillow。 我使用的解决方案是在我的机器(win7 64位)上手动编译zlib库。 然后手动编制枕头包。我像你一样编辑了Setup.py,指向我放置我刚编译的zlib的目录。

我建议您手动编译jpeg lib,然后再使用zlib编译Pillow。确保在执行之前卸载以前版本的PIL / Pillow。

关于如何使用zlib进行详细解答的链接是:

https://stackoverflow.com/a/17190972/2501083

希望这有帮助