<i id='MJXwk'><tr id='MJXwk'><dt id='MJXwk'><q id='MJXwk'><span id='MJXwk'><b id='MJXwk'><form id='MJXwk'><ins id='MJXwk'></ins><ul id='MJXwk'></ul><sub id='MJXwk'></sub></form><legend id='MJXwk'></legend><bdo id='MJXwk'><pre id='MJXwk'><center id='MJXwk'></center></pre></bdo></b><th id='MJXwk'></th></span></q></dt></tr></i><div id='MJXwk'><tfoot id='MJXwk'></tfoot><dl id='MJXwk'><fieldset id='MJXwk'></fieldset></dl></div>

      <small id='MJXwk'></small><noframes id='MJXwk'>

    1. <legend id='MJXwk'><style id='MJXwk'><dir id='MJXwk'><q id='MJXwk'></q></dir></style></legend>
      1. <tfoot id='MJXwk'></tfoot>

          <bdo id='MJXwk'></bdo><ul id='MJXwk'></ul>

        如何将 Python tkinter canvas postscript 文件转换为 PIL 可读的图像文件?

        How to convert a Python tkinter canvas postscript file to an image file readable by the PIL?(如何将 Python tkinter canvas postscript 文件转换为 PIL 可读的图像文件?)

      2. <tfoot id='yP7dT'></tfoot>
              <tbody id='yP7dT'></tbody>
            <i id='yP7dT'><tr id='yP7dT'><dt id='yP7dT'><q id='yP7dT'><span id='yP7dT'><b id='yP7dT'><form id='yP7dT'><ins id='yP7dT'></ins><ul id='yP7dT'></ul><sub id='yP7dT'></sub></form><legend id='yP7dT'></legend><bdo id='yP7dT'><pre id='yP7dT'><center id='yP7dT'></center></pre></bdo></b><th id='yP7dT'></th></span></q></dt></tr></i><div id='yP7dT'><tfoot id='yP7dT'></tfoot><dl id='yP7dT'><fieldset id='yP7dT'></fieldset></dl></div>
            • <bdo id='yP7dT'></bdo><ul id='yP7dT'></ul>
              <legend id='yP7dT'><style id='yP7dT'><dir id='yP7dT'><q id='yP7dT'></q></dir></style></legend>

                1. <small id='yP7dT'></small><noframes id='yP7dT'>

                  本文介绍了如何将 Python tkinter canvas postscript 文件转换为 PIL 可读的图像文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  所以我在我的程序中创建了一个函数,允许用户将他/她在 Turtle 画布上绘制的任何内容保存为带有他/她自己名字的 Postscript 文件.但是,根据 Postscript 文件的性质,有些颜色不会出现在输出中,而且 Postscript 文件也不会在其他一些平台上打开.所以我决定将 postscript 文件保存为 JPEG 图像,因为 JPEG 文件应该能够在许多平台上打开,希望可以显示画布的所有颜色,并且它应该具有比 postscript 文件更高的分辨率.因此,为此,我尝试使用 PIL 在我的保存功能中执行以下操作:

                  So I have created a function in my program that allows the user to save whatever he/she draws on the Turtle canvas as a Postscript file with his/her own name. However, there have been issues with some colors not appearing in the output as per the nature of Postscript files, and also, Postscript files just won't open on some other platforms. So I have decided to save the postscript file as a JPEG image since the JPEG file should be able to be opened on many platforms, can hopefully display all the colors of the canvas, and it should have a higher resolution than the postscript file. So, to do that, I have tried, using the PIL, the following in my save function:

                  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')
                  

                  但是,每当我运行它时,我都会收到此错误:

                  However, whenever I run this, I get this error:

                  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 用户的任何帮助!

                  Apparently it cannot read the postscript file since it's not, according to what I know, an image in itself, so it has to first be converted into an image, THEN read as an image, and then finally converted and saved as a JPEG file. The question is, how would I be able to first convert the postscript file to an image file INSIDE the program possibly using the Python Imaging Library? Looking around SO and Google has been no help, so any help from the SO users is greatly appreciated!

                  遵循 unubuntu 的 建议,我现在有这个用于我的保存功能:

                  Following unubuntu's advice, I have now have this for my save function:

                  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')
                  

                  但是,现在每当我运行它时,我都会收到此错误:

                  However, now whenever I run that, I get this error:

                  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'?为什么我现在会收到此错误?

                  What is 'gs' and why am I getting this error now?

                  推荐答案

                  如果您在调用 cnv.postscript 时不提供文件参数,则cnv.postscript 将 PostScript 作为(unicode)字符串返回.然后,您可以将 unicode 转换为字节并将其提供给 io.BytesIO 并将其提供给 Image.open.Image.open 可以接受任何实现 readseektell 方法的类文件对象作为其第一个参数.

                  If you don't supply the file parameter in the call to cnv.postscript, then a cnv.postscript returns the PostScript as a (unicode) string. You can then convert the unicode to bytes and feed that to io.BytesIO and feed that to Image.open. Image.open can accept as its first argument any file-like object that implements read, seek and tell methods.

                  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.罗达斯的代码,

                  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()
                  

                  这篇关于如何将 Python tkinter canvas postscript 文件转换为 PIL 可读的图像文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)
                  <tfoot id='ljT7C'></tfoot>
                2. <small id='ljT7C'></small><noframes id='ljT7C'>

                3. <i id='ljT7C'><tr id='ljT7C'><dt id='ljT7C'><q id='ljT7C'><span id='ljT7C'><b id='ljT7C'><form id='ljT7C'><ins id='ljT7C'></ins><ul id='ljT7C'></ul><sub id='ljT7C'></sub></form><legend id='ljT7C'></legend><bdo id='ljT7C'><pre id='ljT7C'><center id='ljT7C'></center></pre></bdo></b><th id='ljT7C'></th></span></q></dt></tr></i><div id='ljT7C'><tfoot id='ljT7C'></tfoot><dl id='ljT7C'><fieldset id='ljT7C'></fieldset></dl></div>

                      <tbody id='ljT7C'></tbody>
                    <legend id='ljT7C'><style id='ljT7C'><dir id='ljT7C'><q id='ljT7C'></q></dir></style></legend>

                          <bdo id='ljT7C'></bdo><ul id='ljT7C'></ul>