How can I convert a .ps file into a .png file?

断了今生、忘了曾经 提交于 2021-01-29 10:48:52

问题


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


回答1:


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)


来源:https://stackoverflow.com/questions/62053750/how-can-i-convert-a-ps-file-into-a-png-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!