【发布时间】:2013-11-04 23:27:45
【问题描述】:
我注意到 PNG 图像没有显示在使用 OS X Mavericks 中的 ImageTk.PhotoImage 的 Tkinter 应用程序中。但是,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 系统。
Update 2 Pillow 设置摘要
我安装了 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。
Update 3 尝试构建 Python 2.7.5
也许问题出在 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
【问题讨论】:
-
您是否在 Mountain Lion 上安装了 PIL(和其他东西)然后更新,或者在 Mavericks 上进行了全新安装(或者干净安装了一些东西而不是其他东西,或者构建了
py2app-style Mountain Lion 上的应用程序,然后在 Mavericks 上运行它,或者……)。另外,您使用的是 Apple 预装的 Python 2.7,还是其他一些 Python(如果是后者,是哪个,以及您是如何安装的)?什么版本的 PIL 以及您是如何安装它的? -
您的代码适用于我在 64 位 Python 2.7 上。您使用的是哪个版本的 Python?
-
我问是因为在我的系统上,我在 10.8 下安装了 Python 3.3.2,PNG 工作,然后升级到 10.9,然后
pip-3.3 install --upgrade pillow工作但未能找到 libpng ,所以我最终得到了一个不支持 PNG 的 PIL。我只有在卸载并重新安装枕头并阅读完整输出时才知道这一点。我通过使用 Homebrew 到brew install libpng解决了这个问题(当然,然后重新安装了枕头)。 -
用一些系统细节更新了问题。 @octref 你在小牛队?
-
更一般地说,您可以卸载并重新安装枕头(如果您使用老式 PIL 代替...切换),然后将“PIL 设置摘要”部分粘贴到某处?如果它没有找到 zlib 或 libpng 或 PNG 支持所需的任何其他东西,它会在那里说明。
标签: python macos tkinter python-imaging-library osx-mavericks