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

标签 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

(此代码是伪代码 - 我想知道如何编写一些实际执行此代码看起来会执行的操作的内容。)
提前致谢! Stack 是一个很棒的社区,我很感激。

编辑(尝试解决方案):运行此行时:
os.system("ghostscript file.ps file.png")

我收到以下错误:
'ghostscript' is not recognized as an internal or external command, operable program or batch file.

尝试使用 Pillow 时:
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

Image of Error Message

最佳答案

您可以使用枕头。

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)

关于python - 如何将 .ps 文件转换为 .png 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62053750/

相关文章:

C简单文件整数计数程序实现问题

c# - 如何提高使用 Ghostscript 从 PDF 生成的 JPEG 图像的质量?

linux - 将 pdf 转换为具有相同质量的 Tiff

c++ - Ifstream 读取奇怪的行为

从给定坐标提取 PDF 文本

用于HTML解析的Python正则表达式(BeautifulSoup)

python - 如何在 Python 中使用 sys.exit()

Jquery 和 Django CSRF token

python - 参数的类型是什么,这个函数有什么作用?

java - 如何查找 Zip 文件目录中的任何文件