开发者

Python: PIL - [Errno 32] Broken pipe when saving .png

What I'm trying to do here is save the contents of a Tkinter Canvas as a .png image using PIL.

This is my save function ('graph' is the canvas).

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

But it's getting this error:

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

I'm running t开发者_运维知识库his on Windows 7, Python 2.7.1.

How do I make this work?


oh I just get the same error. I have solve it now

just do the following after installing PIL and Ghostscript

1) Open C:\Python27\Lib\site-packages\PIL\EpsImagePlugin.py 2) Change code near line 50 so that it looks like this:

Build ghostscript command

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

Make sure that gswin32c.exe is in the PATH

good luck


It looks like the Ghostscript executable is erroring out and then closing the connection. Others have had this same problem on different OSes.

So, first I would recommend that you confirm that PIL is installed correctly--see the FAQ page for hints. Next, ensure that Ghostscript is installed and working. Lastly, ensure that Python can find Ghostscript, for example by running a PIL script that works elsewhere.

Oh, also--here are some tips on catching the broken pipe error so your program can be more resilient, recognize the problem, and warn the end-user. Hope that helps!


I have realized that while Python 2.7 has this EPEImagePulgin.py, Anaconda also has it. And unfortunately Anaconda's file is an older version. And unfortunately again, when you run your from Spyder environment it was picking up the epsimageplugin.py file from anaconda folder.

So I was getting similar broken pipe error.

When I went into python 2.7 directory and opened python console and then ran my code, it ran just fine.

Because the lates epsimageplugin.py file takes into consideration the windows environment and appropriate ghostscript exe files. Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜