OS X Mavericks上PIL中的PNG显示损坏了吗? [英] PNG display in PIL broken on OS X Mavericks?

查看:101
本文介绍了OS X Mavericks上PIL中的PNG显示损坏了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到在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.

更新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

解决方案

我认为您的问题是,您的PIL是在Mavericks机器上构建时没有PNG支持或仅部分PNG支持的.

如果PIL找不到所需的libpng和libz,则您将没有完整的PNG支持.从Mountain Lion升级到Mavericks之后,有时候似乎是个问题.

可能与您遇到的问题不同.例如,可能是苹果再次使用越野车版本的Tcl/Tk(就像臭名昭著的Snow Leopard那样).但这绝对值得尝试.

使用 Pillow ,以及某些版本的老式PIL(但实际上,如果您没有使用Pillow,您几乎肯定应该使用),安装过程的最后将为您提供一个友好的"PIL SETUP Summary"部分,向您显示所有重要的配置内容.

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

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

I've noticed that PNG images aren't displaying in Tkinter apps using ImageTk.PhotoImage in OS X Mavericks. But, GIFs and JPEGs display fine. There's no error printed or exception thrown and debugging the code shows the image is read and has the correct height & width. Here's a simplified example:

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()

If you use a GIF the image will be displayed, but using a PNG it will not. Again, this is only happening on OS X Mavericks, Mountain Lion works fine. I've tried re-installing (compiling PIL) with no luck, as well as trying a new virtualenv.

Is there perhaps some PNG attribute I need to set correctly when creating/saving the PNG? Or is this a bug in PIL or Tkinter or OS X?

Update to add some details

I'm using:

  • Python 2.7.5 (/usr/bin/python)
  • PIL 1.1.7 (compiled using pip)

This is on a machine that was just updated to Mavericks from Mountain Lion, and previously had PIL installed, and I haven't messed with the system Python shipped by Apple.

Update 2 Pillow setup summary

I installed Pillow 2.2.1 and it says it has PNG support:

--------------------------------------------------------------------
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
--------------------------------------------------------------------

I also uninstalled and re-installed libpng using brew (libpng 1.5.14). I then re-installed Pillow to make sure it built with it, though I think it uses zlib.

Update 3 trying to build Python 2.7.5

Perhaps the issue is with zlib, trying to compile Python 2.7.5 I get this:

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

解决方案

I think your problem is that your PIL was built without PNG support, or with only partial PNG support, on your Mavericks machine.

If PIL can't find both libpng and libz where it wants to, you won't have complete PNG support. And after upgrading from Mountain Lion to Mavericks, this seems to sometimes be a problem.

This may not be the same problem you're having. For example, it could be down to Apple using a buggy version of Tcl/Tk again (as they infamously did in Snow Leopard). But it's definitely worth trying.

With Pillow, and some versions of old-school PIL (but really, if you're not using Pillow, you almost definitely should be), the end of the install process gives you a friendly "PIL SETUP SUMMARY" section that shows you all the important configuration stuff.

I had the exact same problem with Pillow 2.2.1 on a locally-built Python 3.3.2. I solved it by using Homebrew to install libpng, then rebuilding Pillow:

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

这篇关于OS X Mavericks上PIL中的PNG显示损坏了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆