4

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 this on Windows 7, Python 2.7.1.

How do I make this work?

Holt
  • 36,600
  • 7
  • 92
  • 139

3 Answers3

5

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

rsj217
  • 51
  • 1
  • 2
2

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!

Community
  • 1
  • 1
ewall
  • 27,179
  • 15
  • 70
  • 84
  • I tried opening some EPS scripts with GIMP (which also uses GhostScript) and it can't find it, either, so it must be a problem with GS. I installed it from the link you provided, but it still doesn't work. Any more ideas? –  Jun 09 '11 at 00:56
  • The `gs.exe` and related DLLs etc should added to the system path. If you run `gs.exe --help` on the command line do you get the error message *'gs.exe' is not recognized as an internal or external command, operable program or batch file*? – ewall Jun 09 '11 at 12:26
  • I do get that error -- but I did add a path to the path variable to GhostScript's .exe. Looking at it further, I saw that neither of GhostScript's .exe's have actually are called gs.exe. It has gswin32.exe and gswin32c.exe. I tried renaming them to gs.exe for both of them. It got rid of the command line error that you mentioned, but the Broken Pipe persists. –  Jun 09 '11 at 15:43
  • Ugh! I glanced at the code for the EpsImagePlugin.py, and the comments suggest that it only uses GhostScript on *NIX OSes... but it wasn't immediately clear what other options were. I can do a little more digging tomorrow morning... – ewall Jun 09 '11 at 21:30
  • Oh, sorry man; I'm almost completely out of useful ideas... Have you tried running it in Python's verbose mode (`python -v program.py`)? Similarly, you could try running it thru the [Winpdb debugger](http://winpdb.org/) ([good tutorial here](http://code.google.com/p/winpdb/wiki/DebuggingTutorial); there are, of course, plenty of other options--this just happens to be my favorite cross-platform GUI one). – ewall Jun 16 '11 at 13:03
  • Well... debugging only tells us what the problem is... we know that -- it's a *nix only thing for GS. We just need to fix the problem... not discover it. I really appreciate your help on this... but I might just have to not include this feature. It's not a 100% necessary feature for the program, but it would be nice. –  Jun 17 '11 at 18:46
1

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.

Sastry
  • 11
  • 1