Python 如何将.ps文件转换为.png文件?

Python 如何将.ps文件转换为.png文件?,python,file,ghostscript,Python,File,Ghostscript,我需要将.ps文件转换为.png文件,作为我正在制作的图像识别程序的一部分。我知道我可以使用Ghostscript或其他程序,但有人能给出一个具体的例子,说明如何编写这样的东西: def ps_to_png(ps_file): file = ghostscript.read(ps_file) png_file = ghostscript.save(file, "png") return png_file (这段代码是伪代码——我想知道如何编写一些实际执行这段代码看起来会

我需要将.ps文件转换为.png文件,作为我正在制作的图像识别程序的一部分。我知道我可以使用Ghostscript或其他程序,但有人能给出一个具体的例子,说明如何编写这样的东西:

def ps_to_png(ps_file):
    file = ghostscript.read(ps_file)
    png_file = ghostscript.save(file, "png")
    return png_file
(这段代码是伪代码——我想知道如何编写一些实际执行这段代码看起来会执行的操作的代码。) 提前谢谢!Stack是一个很棒的社区,我很感激

编辑(尝试的解决方案):运行此行时:

os.system("ghostscript file.ps file.png")
我得到以下错误:

'ghostscript' is not recognized as an internal or external command, operable program or batch file.
尝试使用枕头时:

from PIL import Image
def convert_to_png(ps_file):
    img = Image.open(ps_file)
    img.save("img.png")
我得到以下错误:

OSError: Unable to locate Ghostscript on paths
你可以用枕头

from PIL import Image

psimage=Image.open('myImage.ps')
psimage.save('myImage.png')
如果要将其包装为函数:

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)

这看起来是一个很好的解决方案,有什么问题吗?应该解释一下-这只是伪代码-我不知道如何使用ghostscript来执行此功能。使用诸如
os.system(“ghostscript file.ps file.png”)
之类的外部程序怎么样?我很喜欢这个解决方案,但不幸我遇到了一个错误。我编辑了我的文章。当我运行这个程序时,我得到了以下信息:“操作系统错误:无法在路径上找到Ghostscript”Pillow与Ghostscript无关。只需使用pip在python环境中安装并运行它。请注意,您应该更改路径的“myImage.ps”。我编辑了我的帖子,不幸的是,我认为Pillow使用Ghostscript来执行某些功能。我在代码中的任何地方都没有使用ghostscript,只有Pillow。您在PIL脚本中没有使用路径文件。我将把答案改为Clarify谢谢你的帮助,但我的代码与你的示例一模一样。该错误与打开文件无关,而是将其保存为.png。