【发布时间】:2016-01-14 03:29:12
【问题描述】:
我在我的程序中实现了以下save 函数,它允许用户将他/她使用 Turtle 在 Tkinter 画布上绘制的任何内容保存为 JPEG 文件。它的工作原理是它首先捕获屏幕和 Tkinter 画布,然后基于它创建一个 postscript 文件。然后它将 postscript 文件转换为 PIL(Python 图像库)可读文件类型,然后 PIL 将转换后的文件保存为 JPEG。我的保存功能如下图:
def savefirst():
# Capture screen and Tkinter canvas
cnv = getscreen().getcanvas()
global hen
# Save screen and canvas as Postscript file
ps = cnv.postscript(colormode = 'color')
# Open a Tkinter file dialog that allows to input his.her own name for the file
hen = filedialog.asksaveasfilename(defaultextension = '.jpg')
# Convert Postscript file to PIL readable format
im = Image.open(io.BytesIO(ps.encode('utf-8')))
# Finally save converted file as a JPEG
im.save(hen + '.jpg')
但是,每当我运行此保存功能时,都会收到如下所示的错误:
line 2396, in savefirst
im.save(hen + '.jpg')
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/PIL/Image.py", line 1646, in save
self.load()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/PIL/EpsImagePlugin.py", line 337, in load
self.im = Ghostscript(self.tile, self.size, self.fp, scale)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/PIL/EpsImagePlugin.py", line 143, in Ghostscript
stdout=subprocess.PIPE)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 950, in __init__
restore_signals, start_new_session)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 1544, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'gs'
什么是'gs',为什么会发生这种情况,我将如何解决导致此错误的问题?
仅供参考:以防万一,我的操作系统是 Macintosh,我的 Python 版本是 3.5.1
编辑: 显然,正如我所想的那样,'gs' 表示 GhostScript,并且由于我必须安装它而发生该错误(感谢答案让我意识到这一点)。我已经从here 安装了.dmg,但我仍然不断收到错误。也许这与我当前的操作系统(Mac OS 10.11.2)有关?还是我的保存功能有其他问题?老实说,我不知道。 无论如何,即使我(希望)安装了 GhostScript,为什么这个错误仍然出现?再次,非常感谢任何帮助!
【问题讨论】:
标签: python python-3.x canvas tkinter python-imaging-library