因此,我在程序中创建了一个函数,允许用户将他/她在Turtle画布上绘制的任何内容保存为带有他/她自己名字的Postscript文件。但是,由于Postscript文件的性质,有些颜色没有出现在输出中,而且Postscript文件在其他一些平台上无法打开。所以我决定将postscript文件保存为JPEG图像,因为JPEG文件应该可以在许多平台上打开,希望可以显示画布的所有颜色,并且它的分辨率应该比postscript文件高。为此,我在save函数中使用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文件。问题是,我如何才能首先使用Python图像库将postscript文件转换为程序内部的图像文件?环顾苏和谷歌一直没有帮助,所以任何帮助,从苏用户是非常感谢!
编辑:根据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'
为什么我现在得到这个错误?
在对} 可以接受实现
cnv.postscript
的调用中,If you don't supply the file parameter,然后cnv.postscript
将PostScript作为(unicode)字符串返回。 然后,您可以将unicode转换为字节,并将其馈送到io.BytesIO
,然后将其馈送到Image.open
。^{read
、seek
和tell
方法的任何类似文件的对象作为其第一个参数。例如,向A. Rodas' code大量借款
除了unutbu的答案之外,您还可以将数据再次写入BytesIO对象,但这样做之后,您必须寻找缓冲区的开头。下面是一个在浏览器中显示图像的烧瓶示例:
相关问题 更多 >
编程相关推荐