使用 python 在 windows cmd 中执行操作

Using python to carry out actions in the windows cmd

我有一个程序,可以将 canvas 保存到 postscript 文件中。该程序然后使用 IrfanView 打开文件,我可以在其中 手动 将其保存为 .png,然后我可以 运行 来自 python 的另一个函数,它执行另一个操作并再次将其另存为.png。我的问题是是否有办法删除中间的手动位(我必须单击 另存为 按钮)或者是否可以通过 python代码?

到目前为止,我发现我无法保存 canvas 并且上面的任何内容(我使用的是 turtles)只能使用 postscript 保存。

还要从 python 中将 postscript 转换为 png 或 jpeg 似乎也有点困难。

注意: 本质上我使用 Irfan 进行 postscript 到 .png 的转换,但我想对用户隐藏这个过程的步骤,所以它会很好如果程序可以为我做到这一点。

新说明:我尝试使用 python 子进程模块调用 cmd 并使用它进行转换,但每当我尝试 运行 .Popen.call 函数我得到一个错误 - 拒绝访问或找不到文件,无论哪种方式命令都不想从 [=35= 运行 ] 程序。我什至尝试通过 cmd 从 python 打开一个文件,结果却出现错误(直接在 cmd 中输入相同的命令时有效):

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

假设您有一个名为 saved.ps 的 postscript 文件,您想要使用设备 pngalpha 使用 Ghostscript 将其转换为 png 文件,您可以这样做:

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