即使在 Macintosh 上安装 GhostScript,Python <没有这样的文件或目录:'gs'> 错误 *问题仍然存在!*

2024-03-25

我在程序中实现了以下保存功能,该功能允许用户将他/她使用 Turtle 在 Tkinter 画布上绘制的任何内容保存为 JPEG 文件。它的工作原理是首先捕获屏幕和 Tkinter 画布,然后基于它创建一个 postscript 文件。然后,它将 postscript 文件转换为 PIL(Python 图像库)可读文件类型,然后 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 http://pages.uoregon.edu/koch/并运行安装程序。然而,由于这不起作用,我然后在Terminal:

`pip install ghostscript`

因为即使这样也不起作用,然后我在终端中运行了这个:

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

I used 3 ways安装 GhostScript 后仍然出现同样的错误。这是为什么?我还能如何解决这个问题?

仅供参考:以防万一您需要知道,我的操作系统是 Mac OS 10.11.2,我的 Python 版本是 3.5.1

EDIT:因此,正如评论曾经说过的那样,尝试使用 Homebrew 来解决这个问题。所以,我很自然地安装了它(因为我还没有安装 Homebrew)。然后,安装成功后,我运行了brew install ghostscript。在我解决了一些问题之后,我能够成功安装 GhostScript(好吧,无论如何,根据 Homebrew 的说法)。然后我跑了brew doctor并发现它没有链接! ****喘息**** 嗯,修复很简单,只需链接即可!所以,这就是我所做的,在修复了其他一些问题之后,我终于能够成功执行brew link 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

还有什么问题吗?为什么Python(甚至终端)找不到gs?

编辑#2:好的,我终于可以安装 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.

But my 保存功能在 Python 中仍然不起作用,现在我确信我已经安装了 GhostScript!每当我运行保存函数时,我仍然在 IDLE 中不断收到此错误:

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

还是出了什么问题?

编辑#3: Here https://drive.google.com/file/d/0B_2bdLwzG0cHUXF5WmRFRVF1MGc/view?usp=sharing是我的 Python 脚本,以防万一需要。

-编辑 #4 已删除-

编辑#5:我有一种预感,这很重要,所以我把它放在那里。每当我跑步时brew doctor在终端中,我收到并不重要的警告,但由于某种原因,这个警告对我来说很突出。我真的不知道为什么,但无论如何,这是:

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)仍然CANNOT detect gs(也许是因为它的 subprocess.Popen() 函数?)。这是为什么?我的 Macintosh 上安装了 xQuarts、GhostScript 和 PIL, but I 当我运行时仍然出现错误savefirst()功能!Why?

附:这一切都发生在 Macintosh (OS 10.11.2) 上,因此优先考虑精通 Macintosh 的人的答案!

-编辑 #7 已删除-

编辑#8:好吧,我终于能够永久(希望......)添加/usr/local/bin/gs到我的路径(如下所示):

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文件。然而,我仍然继续得到这个FileNotFoundError在Python中!我附加了吗/usr/local/bin/gs我的 PATH 路径错误,甚至位于错误的目录中?如果情况并非如此,那么为什么 Python 图像库仍然找不到gs??

编辑#9:欢呼!问题解决了!该错误不再出现。我所要做的就是删除/usr/local/bin/gs从我的 Mac 路径中,显然将其添加到我的 PYTHONPATH 中,就像我的程序开头一样:

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

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


看来您缺少依赖项。下载homebrew http://brew.sh并从中安装 Ghostscript:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

...

brew install ghostscript

还有一些选项可以使用以下方式获得brew info ghostscript:

--with-djvu
        Build drivers for DjVU file format
--with-x11
        Build with x11 support
--HEAD
        Install HEAD version

但默认安装通常是经过最多测试的。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

即使在 Macintosh 上安装 GhostScript,Python <没有这样的文件或目录:'gs'> 错误 *问题仍然存在!* 的相关文章

随机推荐