因此,我在程序中创建了一个函数,该函数使用户可以将自己在Turtle画布上绘制的任何内容保存为具有自己名字的Postscript文件。但是,根据Postscript文件的性质,存在一些颜色未出现在输出中的问题,并且Postscript文件仅在某些其他平台上无法打开。因此,我决定将后记文件另存为JPEG图像,因为JPEG文件应该可以在许多平台上打开,可以显示画布的所有颜色,并且分辨率应比后记文件高。因此,为此,我尝试在保存功能中使用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文件 到一个 图像文件 INSIDE可能使用Python图像库的计划?环顾SO和Google毫无帮助,因此非常感谢SO用户提供的任何帮助!
编辑: 根据unubuntu's建议,我现在有我的保存功能:
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',为什么现在出现此错误?
'gs'
如果您在对的调用中未提供file参数cnv.postscript,则acnv.postscript返回PostScript作为(unicode)字符串。然后,您可以将unicode转换为字节,并将其提供给io.BytesIO和Image.open。Image.open可以作为第一个参数接受任何类似文件的对象,农具read,seek和tell方法。
cnv.postscript
io.BytesIO
Image.open
read
seek
tell
import io 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')
例如,大量借用A. Rodas的代码,
import Tkinter as tk import subprocess import os import io from PIL import Image class App(tk.Tk): def __init__(self): tk.Tk.__init__(self) self.line_start = None self.canvas = tk.Canvas(self, width=300, height=300, bg="white") self.canvas.bind("<Button-1>", lambda e: self.draw(e.x, e.y)) self.button = tk.Button(self, text="save", command=self.save) self.canvas.pack() self.button.pack(pady=10) def draw(self, x, y): if self.line_start: x_origin, y_origin = self.line_start self.canvas.create_line(x_origin, y_origin, x, y) self.line_start = x, y def save(self): ps = self.canvas.postscript(colormode='color') img = Image.open(io.BytesIO(ps.encode('utf-8'))) img.save('/tmp/test.jpg') app = App() app.mainloop()