0

I have a program, which saves a canvas into a postscript file. The program then opens the file with IrfanView, where I can manually save it as a .png and then I can run another function from python, which does another operation with it and saves it as a .png again. My question is whether there is a way to either cut out the middle manual bit (where I have to click the save as button) or whether the saving from IrfanView can all be done through python code?

This far I've found out that I cannot save the canvas and whatever is on it (im using turtles) can only be saved using postscript.

Also converting postscript to png or jpeg from within python also seems to be a bit of a tall order.

Note: Essentially I use Irfan to do the postscript to .png conversion, but I would like to hide this step of the process from the user, so it would be nice if the program could do it for me.

New Note: I have tried to use the python subprocess module to make a call to the cmd and use that to convert, but whenever I attempt to run the .Popen or the .call function I get an error - Access denied or file not found, either way the commands don't want to run from the python program. I even tried just opening a file from python, through the cmd only to get an error (the same command works when typed directly into the cmd):

WindowsError: [Error 193] %1 is not a valid Win32 application

Oliver
  • 821
  • 5
  • 12
  • 28
  • Isn't there a command line option? – Peter Wood Oct 21 '15 at 08:25
  • I've seen there are a few command line options to convert from ps to png using ImageMagick/GhostScript, but I am not quite sure how to run those from code? – Oliver Oct 21 '15 at 08:27
  • http://stackoverflow.com/questions/28856297/converting-postscript-to-an-image - I've seen this, but how do I run this from within my python script? – Oliver Oct 21 '15 at 08:28
  • Maybe try a different option. [This solution](http://stackoverflow.com/a/25051183/1084416) uses [**`cairosvg`**](http://cairosvg.org/) and [**`canvasvg`**](https://pypi.python.org/pypi/canvasvg/1.0.0) – Peter Wood Oct 21 '15 at 08:41
  • Ghostscript has a command line interface, so you can simply run it from Python through the subprocess module. Alternatively, there is a python-ghostscript project in pypi, but it has only a beta status. – Serge Ballesta Oct 21 '15 at 09:23
  • so the subprocess module will allow me to run cmd commands from a python script? – Oliver Oct 21 '15 at 09:26
  • I tried the subprocess module and I can feel I'm getting closer, however when using `args = ["./","convert","saved.ps newsave.png"]` or `args = ["./","gs","-o output.png -sDEVICE=pngalpha saved.ps"] subprocess.Popen(args)` I get a Access is Denied error – Oliver Oct 21 '15 at 09:51

1 Answers1

0

Assuming that you have a postscript file named saved.ps that you want to convert to a png file with Ghostscript using the device pngalpha, you could do:

gspath = "/path/to/gs" # would be gspath="c:\path\to\gswin32c" on Windows...
infile = "saved.ps"
outfile = "output.png"
gs = subprocess.Popen(["gs", "-o", "output.png", "-sDEVICE=pngalpha",
              "-dBatch", infile], executable=gspath,
              stdout=subprocess.PIPE, stderr = subprocess.PIPE)
out, err = gs.communicate()

if gs.returncode != 0:
    # do error processing, at least display out and err
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • will this work on a windows device? I have been trying to do it using the ImageMagick `convert`, but I ran into a different problem - http://stackoverflow.com/questions/33282658/imagemagick-conversion-from-ps-to-png-run-from-python-invalid-param – Oliver Oct 22 '15 at 14:09