4

I need to convert .ps files to .png files as part of an image recognition program I am making. I know I can use Ghostscript or other programs, but could someone give a specific example of how to write something like this:

def ps_to_png(ps_file):
    file = ghostscript.read(ps_file)
    png_file = ghostscript.save(file, "png")
    return png_file

(This code is pseudo code- I want to know how to write something that actually does what this code looks like it will do.) Thanks in advance! Stack is a great community and I appreciate it.

EDIT (Attempted solutions): When running this line:

os.system("ghostscript file.ps file.png")

I get the following Error:

'ghostscript' is not recognized as an internal or external command, operable program or batch file.

When attempting to use Pillow:

from PIL import Image
def convert_to_png(ps_file):
    img = Image.open(ps_file)
    img.save("img.png")

I get the following error:

OSError: Unable to locate Ghostscript on paths

Image of Error Message

InstaK0
  • 342
  • 3
  • 9
  • This looks like a great solution, what is wrong with it? – tiberius May 27 '20 at 22:30
  • Should've explained- that is simply pseudo-code- I don't know how to use ghostscript to perform this functionality. – InstaK0 May 27 '20 at 22:31
  • how about using external program like `os.system("ghostscript file.ps file.png")` – furas May 27 '20 at 22:49
  • I would love that solution, but unfortunately I get an error. I edited my post. – InstaK0 May 27 '20 at 23:02
  • Just install ghostscript, then using "from PIL import EpsImagePlugin EpsImagePlugin.gs_windows_binary = r'C:\Program Files\gs\gs9.55.0\bin\gswin64c'" to solve the error. – eee Dec 20 '21 at 11:12

1 Answers1

3

You can use Pillow.

from PIL import Image

psimage=Image.open('myImage.ps')
psimage.save('myImage.png')

If you want to wrap it to a function:

from PIL import Image

def convert_to_png(path):
    img = Image.open(path)
    img.save("img.png")

path='/path_to_your_file'
convert_to_png(path)
Henrique
  • 328
  • 1
  • 2
  • 10
  • 1
    When I run this, I get the following: "OS Error: Unable to locate Ghostscript on paths" – InstaK0 May 27 '20 at 23:08
  • Pillow has nothing to do with Ghostscript. Just install it at your python env with pip and run it. Note that you should change 'myImage.ps' for your path. – Henrique May 27 '20 at 23:11
  • I edited my post, and unfortunately i think Pillow uses Ghostscript to perform certain functions. I am not using ghostscript anywhere in my code, only Pillow. – InstaK0 May 27 '20 at 23:12
  • you are not using path file on your PIL script. I'll change my answer to clarify – Henrique May 27 '20 at 23:14
  • Thanks for the help, but my code is exactly like your example. The error doesn't have anything to do with opening the file, but saving it as a .png. – InstaK0 May 27 '20 at 23:21
  • so maybe your error is that your Ghostscript is not at your system path. Did you try to reinstall it? Did you try to run your code outside IDE? Directly from terminal ? – Henrique May 27 '20 at 23:26
  • No I can't figure out how to do it. "It isn't a recognizable batch or file" even though pip install ghostscript says " Requirement already satisfied" – InstaK0 May 27 '20 at 23:36
  • i think your problem is with system path. I see you are using windows, maybe this answer can help you: https://stackoverflow.com/a/6015004/8738174 – Henrique May 28 '20 at 00:01
  • Sure thing. I am glad it helps. – Henrique May 28 '20 at 23:03