【发布时间】:2016-04-19 01:49:15
【问题描述】:
所以我在我的程序中创建了一个函数,允许用户将他/她在 Turtle 画布上绘制的任何内容保存为带有他/她自己的名字的 Postscript 文件。但是,根据 Postscript 文件的性质,有些颜色不会出现在输出中,而且 Postscript 文件也不会在其他一些平台上打开。所以我决定将 postscript 文件保存为 JPEG 图像,因为 JPEG 文件应该能够在许多平台上打开,希望可以显示画布的所有颜色,并且它应该具有比 postscript 文件更高的分辨率。因此,为此,我尝试使用 PIL 在我的保存功能中执行以下操作:
def savefirst():
cnv = getscreen().getcanvas()
global hen
fev = cnv.postscript(file = 'InitialFile.ps', colormode = 'color')
hen = filedialog.asksaveasfilename(defaultextension = '.jpg')
im = Image.open(fev)
print(im)
im.save(hen + '.jpg')
但是,每当我运行它时,我都会收到此错误:
line 2391, in savefirst
im = Image.open(fev)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/PIL/Image.py", line 2263, in open
fp = io.BytesIO(fp.read())
AttributeError: 'str' object has no attribute 'read'
显然它无法读取 postscript 文件,因为它 不是,据我所知,它本身就是一个图像,所以它必须首先转换为图像,然后作为图像读取,然后然后最后转换并保存为JPEG文件。 问题是,我如何能够首先将 postscript 文件 转换为 图像文件 在可能使用 Python 图像库的程序内部?环顾 SO 和 Google 没有任何帮助,因此非常感谢 SO 用户的任何帮助!
编辑:遵循unubuntu's 的建议,我现在有了这个用于我的保存功能:
def savefirst():
cnv = getscreen().getcanvas()
global hen
ps = cnv.postscript(colormode = 'color')
hen = filedialog.asksaveasfilename(defaultextension = '.jpg')
im = Image.open(io.BytesIO(ps.encode('utf-8')))
im.save(hen + '.jpg')
但是,现在每当我运行它时,我都会收到此错误:
line 2395, 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',为什么我现在会收到此错误?
【问题讨论】:
标签: python python-3.x canvas save python-imaging-library