即使在Macintosh上安装了GhostScript也会出现Python错误*问题仍然存在!*

我在我的程序中实现了以下保存功能,允许用户将他/她在Tkinter画布上绘制的任何内容保存为JPEG文件.它应该如何工作是它首先捕获屏幕和Tkinter画布,然后基于它创建一个postscript文件.然后它将该postscript文件转换为PIL( Python Imaging Library)可读文件类型,然后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'

即使我已经安装了GhostScript!我知道这是因为我首先通过位于here的磁盘映像安装它并运行安装程序.但是,由于那不起作用,我接着在终端做了这个:

`pip install ghostscript`

因为即使这不起作用,我在终端运行:

`conda install --channel https://conda.anaconda.org/mlgill ghostscript`

我使用了3种方法来安装GhostScript并仍然保持相同的错误.为什么这样,我怎么解决这个问题呢?

仅供参考:如果你需要知道,我的操作系统是Mac OS 10.11.2,我的Python版本是3.5.1

编辑:所以,作为评论曾经说过,尝试使用Homebrew来解决这个问题.所以,我自然安装了它(因为我没有安装Homebrew).然后,成功安装后,我运行brew install ghostscript.在我解决了几个问题之后,我能够成功安装GhostScript(好吧,根据Homebrew反正).然后我跑了brew医生,发现它没有联系! ****喘气****嗯,修复很容易,只需链接它!所以,这就是我所做的,在修复了其他几个问题后,我终于能够成功执行brew链接ghostscript.然而,尽管我在修复错误方面做出了英勇而艰难的尝试,但它仍然存在……是的,尽管我付出了最大的努力,但当我运行保存功能时IDLE中的错误仍然会发生!此外,在终端中运行gs返回:

dyld: Library not loaded: /opt/X11/lib/libXt.6.dylib
  Referenced from: //anaconda/bin/gs
  Reason: image not found
Trace/BPT trap: 5

什么是STILL出错了?为什么Python(甚至终端)不能找到gs?

编辑#2:好的,所以我终于能够使用Homebrew安装GhostScript了.我所要做的就是安装xQuarts,GhostScript显然需要它.但是,我的初始问题仍然没有解决! gs现在在终端工作:

:~ #######$gs
GPL Ghostscript 9.18 (2015-10-05)
Copyright (C) 2015 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.

但我的保存功能STILL在Python中不起作用,现在我确定我安装了GhostScript!每当我运行保存功能时,我仍然继续在IDLE中收到此错误:

FileNotFoundError: [Errno 2] No such file or directory: 'gs'

还有什么问题?

编辑#3:Here是我的Python脚本,以防万一需要它.

-EDIT#4删除 –

编辑#5:我有一个小预感,这很重要,所以我把它放在那里.每当我在终端运行brew医生时,我都会收到不重要的警告,但是出于某种原因这个警告很突出.我真的不知道为什么,但无论如何,这里是:

Warning: Python is installed at /Library/Frameworks/Python.framework

Homebrew only supports building against the System-provided Python or a
brewed Python. In particular, Pythons installed to /Library can interfere
with other software installs.

也许这是导致我的问题的原因?如果是这样,我该如何解决?

编辑#6:显然它适用于其他人(包括Windows和Mac),但仍然不适合我!每当我运行程序的savefirst()函数时,我仍然继续收到此错误:

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在终端工作并且这个:

x=subprocess.Popen(['/usr/local/bin/gs', '--version'], stdout=subprocess.PIPE)
print(x.stdout.read())

适用于Python IDLE.但由于某些原因,Python成像库(PIL)仍然无法检测到gs(可能是因为它的subprocess.Popen()函数?).这是为什么?我在我的Macintosh上安装了xQuarts,GhostScript和PIL,但是当我运行savefirst()函数时仍然出现错误!为什么?

附:这一切都发生在Macintosh(OS 10.11.2)上,所以熟悉Macintosh的人的答案是首选!

-EDIT#7删除 –

编辑#8:好的,我终于能够永久地(希望……)将/usr/local/bin / gs添加到我的PATH中(如下所示):

Library/usr/local/bin/gs:/Library/Frameworks/Python.framework/Versions/3.5/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin://anaconda/bin:/Users/Rohan/anaconda/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin

我通过运行pico~MyName / .bash_profile添加了该路径,然后输入:

PATH="Library/usr/local/bin/gs:${PATH}"
export PATH

进入/.bash.profile文件.但是,我仍然继续在Python中获取FileNotFoundError!我是以错误的方式将/usr/local/bin / gs附加到我的PATH,还是在错误的目录中?如果不是这样,那么为什么Python Imaging Library STILL不能找到gs?

编辑#9:华友世纪!问题解决了!该错误不再出现.我所要做的就是从我的Mac Path中删除/usr/local/bin / gs,然后显然将它添加到我的PYTHONPATH中,就像在程序开始时一样:

os.environ["PATH"] += ":/usr/local/bin:/usr/local/bin/gs"

好哇!有用! PIL现在可以找到gs,并且可以将画布保存为JPEG,虽然它的分辨率非常低,但这是另一个问题的问题.现在,我很高兴初始问题得到解决,我终于可以将画布保存为JPEG(感谢@Copperfield!):)

最佳答案 只是一个想法:检查PIL的 code,它使用 shutil模块找到gs做shutil.which(“gs”)也许你需要修改一些环境变量,直到那个工作?

编辑

在聊天室里聊天,我们发现似乎是什么问题,那就是subprocess.Popen找不到gs,但是我们卡在那里.有任何想法吗??

编辑2

我找到了一个可行的解决方案,首先我从我的路径中移除了gs,然后我尝试了这个

>>> import os,shutil,subprocess
>>> shutil.which("gswin64c") # shutil.which("gs")
>>> test = subprocess.Popen(["gswin64c","--version"],stdout=subprocess.PIPE) # subprocess.Popen(["gs","--version"],stdout=subprocess.PIPE)
Traceback (most recent call last): 
  File "<pyshell#3>", line 1, in <module>
    test = subprocess.Popen(["gswin64c","--version"],stdout=subprocess.PIPE)
  File "C:\Anaconda3\lib\subprocess.py", line 950, in __init__
    restore_signals, start_new_session)
  File "C:\Anaconda3\lib\subprocess.py", line 1220, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] El sistema no puede encontrar el archivo especificado
>>> os.environ["PATH"]
'C:\\Anaconda3\\Library\\bin;C:\\Anaconda3\\Library\\bin;C:\\Anaconda3;C:\\Anaconda3\\Scripts;C:\\Anaconda3\\Library\\bin;C:\\ProgramData\\Oracle\\Java\\javapath;C:\\Program Files (x86)\\Haskell\\bin;C:\\Program Files (x86)\\Haskell Platform\\2013.2.0.0\\lib\\extralibs\\bin;C:\\Program Files (x86)\\Haskell Platform\\2013.2.0.0\\bin;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files (x86)\\nodejs\\;C:\\Program Files\\MATLAB\\R2009a\\bin;C:\\Program Files\\MATLAB\\R2009a\\bin\\win64;C:\\Program Files (x86)\\Haskell Platform\\2013.2.0.0\\mingw\\bin;C:\\Program Files\\MiKTeX 2.9\\miktex\\bin\\x64\\;C:\\Program Files (x86)\\Skype\\Phone\\;C:\\Anaconda3;C:\\Anaconda3\\Scripts;C:\\Anaconda3\\Library\\bin;C:\\Users\\David\\AppData\\Roaming\\cabal\\bin;C:\\Users\\David\\AppData\\Roaming\\npm;C:\\Program Files (x86)\\Java\\jre7\\bin;C:\\Users\\David\\Dropbox\\Programas Random'
>>> os.environ["PATH"] += ";C:\\Program Files\\gs\\gs9.18\\bin"
>>> shutil.which("gswin64c")
'C:\\Program Files\\gs\\gs9.18\\bin\\gswin64c.EXE'
>>> test = subprocess.Popen(["gswin64c","--version"],stdout=subprocess.PIPE)
>>> test.stdout.read()
b'9.18\n'
>>> 

为了使你的代码能够使用我做的以下更改:

#this is very first import
import os
print("## Addind gs to environ ##", os.environ["PATH"] )
os.environ["PATH"] += ";C:\\Program Files\\gs\\gs9.18\\bin"
print("## Addind gs to environ ##", os.environ["PATH"] )

#then the others import, and everything else

并测试它工作正常.

在你的情况下,可能是:

#this is very first import
import os
print("## Addind gs to environ ##", os.environ["PATH"] )
os.environ["PATH"] += ":/usr/local/bin" 
print("## Addind gs to environ ##", os.environ["PATH"] )
#then the others import, and everything else

是不起作用试试其中之一

os.environ["PATH"] += "Library/usr/local/bin" 
os.environ["PATH"] = "/usr/local/bin:" + os.environ["PATH"]
os.environ["PATH"] = "Library/usr/local/bin:" + os.environ["PATH"]

编辑3

随着这个解决方案的工作,这个更自动的版本可以是这样的:

#this is the very first thing to do
import os, subprocess, shutil
#see is gs is avaible
if shutil.which("gs") is None:
   print("GhostScrip is not avaible, search for it")
   try:
      gs = subprocess.Popen(["which","gs"],stdout=subprocess.PIPE)
      gs_path = gs.stdout.read()
      gs_path = gs_path.decode() if isinstance(gs_path,bytes) else gs_path
      print("GhostScrip found in",gs_path)
      os.environ["PATH"] += ":"+ os.path.dirname(gs_path)
   except Exception as e:
      raise Warning("GhostScrip not found, this program may fail")

del subprocess
del shutil

#then everything else
点赞