【问题标题】:Python: PIL - [Errno 32] Broken pipe when saving .pngPython:PIL - [Errno 32] 保存 .png 时管道损坏
【发布时间】:2011-09-11 00:35:02
【问题描述】:

我在这里尝试做的是使用 PIL 将 Tkinter Canvas 的内容保存为 .png 图像。

这是我的保存功能('graph' 是画布)。

def SaveAs():
    filename = tkFileDialog.asksaveasfilename(initialfile="Untitled Graph", parent=master)
    graph.postscript(file=filename+".eps")
    img = Image.open(filename+".eps")
    img.save(filename+".png", "png")

但它得到了这个错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "C:\Users\Adam\Desktop\Graphing Calculator\Graphing Calculator.py", line 352, in SaveAs
    img.save(filename+".png", "png")
  File "C:\Python27\lib\site-packages\PIL\Image.py", line 1406, in save
    self.load()
  File "C:\Python27\lib\site-packages\PIL\EpsImagePlugin.py", line 283, in load
    self.im = Ghostscript(self.tile, self.size, self.fp)
  File "C:\Python27\lib\site-packages\PIL\EpsImagePlugin.py", line 72, in Ghostscript
    gs.write(s)
IOError: [Errno 32] Broken pipe

我在 Windows 7,Python 2.7.1 上运行它。

我该如何进行这项工作?

【问题讨论】:

    标签: python tkinter python-imaging-library tkinter-canvas


    【解决方案1】:

    哦,我也遇到了同样的错误。我已经解决了

    安装 PIL 和 Ghostscript 后只需执行以下操作

    1) 打开 C:\Python27\Lib\site-packages\PIL\EpsImagePlugin.py 2) 更改第 50 行附近的代码,使其如下所示:

    构建 ghostscript 命令

    command = ["gswin32c",
               "-q",                    # quite mode
               "-g%dx%d" % size,        # set output geometry (pixels)
               "-dNOPAUSE -dSAFER",     # don't pause between pages, safe mode
               "-sDEVICE=ppmraw",       # ppm driver
               "-sOutputFile=%s" % file,# output file
               "-"
               ]
    

    确保 gswin32c.exe 在 PATH 中

    祝你好运

    【讨论】:

      【解决方案2】:

      它看起来像 Ghostscript executable is erroring out and then closing the connection。其他人在different OSes 上有这个same problem

      所以,首先我建议您确认 PIL 已正确安装 - 请参阅 FAQ page 以获取提示。接下来,确保Ghostscript 已安装并正常工作。最后,确保 Python 可以找到 Ghostscript,例如通过运行在其他地方工作的 PIL 脚本。

      哦,还有--这里有some tips on catching the broken pipe error,这样您的程序可以更有弹性,识别问题并警告最终用户。希望对您有所帮助!

      【讨论】:

      • 我尝试用GIMP(也使用GhostScript)打开一些EPS脚本,也找不到,所以肯定是GS的问题。我从您提供的链接安装了它,但它仍然无法正常工作。还有什么想法吗?
      • gs.exe 和相关的 DLL 等应该添加到系统路径中。如果您在命令行上运行gs.exe --help,您会收到错误消息'gs.exe' is not identify as an internal or external command, operable program or batch file
      • 我确实得到了那个错误——但我确实将路径变量的路径添加到了 GhostScript 的 .exe。进一步看,我发现 GhostScript 的 .exe 实际上都没有被称为 gs.exe。它有 gswin32.exe 和 gswin32c.exe。我尝试将它们重命名为 gs.exe。它摆脱了您提到的命令行错误,但Broken Pipe仍然存在。
      • 呃!我看了一眼 EpsImagePlugin.py 的代码,cmets 建议它只在 *NIX 操作系统上使用 GhostScript……但目前还不清楚还有哪些其他选项。明天早上我可以多挖一点……
      • 哦,对不起,伙计;我几乎完全没有有用的想法...您是否尝试过以 Python 的详细模式 (python -v program.py) 运行它?同样,您可以尝试通过Winpdb debugger 运行它(good tutorial here;当然,还有很多其他选项——这恰好是我最喜欢的跨平台 GUI 之一)。
      【解决方案3】:

      我意识到虽然 Python 2.7 有这个 EPEImagePulgin.py,但 Anaconda 也有它。不幸的是,Anaconda 的文件是旧版本。不幸的是,当您从 Spyder 环境运行时,它会从 anaconda 文件夹中获取 epsimageplugin.py 文件。

      所以我遇到了类似的断管错误。

      当我进入 python 2.7 目录并打开 python 控制台然后运行我的代码时,它运行得很好。

      因为lates epsimageplugin.py 文件考虑了windows 环境和适当的ghostscript exe 文件。希望这可以帮助。

      【讨论】:

        猜你喜欢
        • 2021-01-04
        • 2017-04-22
        • 2014-04-28
        • 1970-01-01
        • 2013-04-16
        • 2021-10-11
        • 1970-01-01
        • 2012-12-21
        • 2016-04-30
        相关资源
        最近更新 更多