在OS X Mavericks中打破PIL的PNG显示?

时间:2013-10-25 18:10:22

标签: python macos tkinter python-imaging-library osx-mavericks

我注意到在OS X Mavericks中使用ImageTk.PhotoImage在Tkinter应用中不显示PNG图像。但是,GIF和JPEG显示正常。没有打印错误或抛出异常并且调试代码显示图像被读取并具有正确的高度&宽度。这是一个简化的例子:

import Tkinter
from PIL import Image, ImageTk

logo_file = 'test.png'
#logo_file = 'test.gif'


class Application(Tkinter.Frame):
    def __init__(self, master):
        Tkinter.Frame.__init__(self, master)
        self.master.minsize(width=512, height=256)
        self.master.config()

        self.pack()

        self.main_frame = Tkinter.Frame()
        self.some_image = ImageTk.PhotoImage(Image.open(logo_file))

        some_label = Tkinter.Label(self.main_frame, image=self.some_image)
        some_label.config()
        some_label.pack(side='top')

        self.main_frame.place(in_=self.master, anchor='c', relx=.5, rely=.5)

root = Tkinter.Tk()
app = Application(root)
app.mainloop()

如果使用GIF,将显示图像,但使用PNG则不会。再次,这只发生在OS X Mavericks上,Mountain Lion工作正常。我已经尝试重新安装(编译PIL)没有运气,以及尝试新的virtualenv。

在创建/保存PNG时,是否需要正确设置一些PNG属性?或者这是PIL或Tkinter或OS X中的错误?

更新以添加一些细节

我正在使用:

  • Python 2.7.5(/ usr / bin / python)
  • PIL 1.1.7(使用pip编译)

这是在一台刚从Mountain Lion更新到Mavericks的机器上,以前安装了PIL,我没有搞乱Apple提供的系统Python。

更新2个枕头设置摘要

我安装了Pillow 2.2.1,它说它有PNG支持:

--------------------------------------------------------------------
PIL SETUP SUMMARY
--------------------------------------------------------------------
version      Pillow 2.2.1
platform     darwin 2.7.5 (default, Aug 25 2013, 00:04:04)
             [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)]
--------------------------------------------------------------------
--- TKINTER support available
--- JPEG support available
--- ZLIB (PNG/ZIP) support available
--- TIFF G3/G4 (experimental) support available
--- FREETYPE2 support available
*** LITTLECMS support not available
*** WEBP support not available
*** WEBPMUX support not available
--------------------------------------------------------------------

我还使用brew(libpng 1.5.14)卸载并重新安装了libpng。然后我重新安装Pillow以确保它是用它构建的,尽管我认为它使用了zlib。

尝试构建Python 2.7.5的更新3

也许问题出在zlib上,尝试编译Python 2.7.5我得到了这个:

Python build finished, but the necessary bits to build these modules were not found:
_bsddb             _sqlite3           _ssl             
bsddb185           dbm                dl               
gdbm               imageop         linuxaudiodev    
nis                ossaudiodev        readline        
spwd               sunaudiodev        zlib
To find the necessary bits, look in setup.py in detect_modules() for the module's name.


Failed to build these modules:
_tkinter

4 个答案:

答案 0 :(得分:4)

我认为您的问题是您的PIL是在没有PNG支持的情况下构建的,或者在您的Mavericks机器上仅支持部分PNG。

如果PIL找不到它想要的libpng和libz,你将无法获得完整的PNG支持。从Mountain Lion升级到Mavericks后,这似乎有时会成为一个问题。

可能与您遇到的问题不同。例如,它可能会再次使用一个有缺陷的Tcl / Tk版本(因为它们在Snow Leopard中臭名昭着)。但这绝对值得一试。

使用Pillow和某些版本的老派PIL(但实际上,如果你没有使用Pillow,你几乎肯定应该这样),安装过程的结束会给你一个友好的“PIL SETUP”摘要“部分显示了所有重要的配置。

我在本地构建的Python 3.3.2上遇到与Pillow 2.2.1完全相同的问题。我通过使用Homebrew来安装libpng,然后重建Pillow来解决它:

$ brew install libpng
$ pip-3.3 uninstall pillow
$ pip-3.3 install pillow

答案 1 :(得分:1)

我遇到了同样的问题,我做了以下事情。这似乎是固定的。

sudo unistall pillow
xcode-select --install
pip install pillow

似乎工作得很好。我还安装了下面的所有项目,但似乎你已经安装了

brew install libtiff libjpeg libpng webp littlecms 

答案 2 :(得分:0)

您是否安装了Pillow依赖项?

pip uninstall Pillow
brew install libtiff libjpeg webp littlecms
pip install Pillow

您需要HomeBrew来执行brew命令。

答案 3 :(得分:0)

我从自制软件(brew install Pillow)安装了Pillow,将自动安装所有必需的依赖项。问题解决了。

相关问题