R. *_*Kap 5 python macos save ghostscript python-3.x
我在我的程序中实现了以下保存功能,它允许用户将他/她用 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')
Run Code Online (Sandbox Code Playgroud)
但是,每当我运行此保存功能时,都会收到如下所示的错误:
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'
Run Code Online (Sandbox Code Playgroud)
即使我已经安装了 GhostScript!我知道这一点是因为我首先通过位于此处的磁盘映像安装它并运行安装程序。但是,由于那不起作用,然后我在终端中执行了此操作:
`pip install ghostscript`
Run Code Online (Sandbox Code Playgroud)
因为即使这样也不起作用,我然后在终端中运行了这个:
`conda install --channel https://conda.anaconda.org/mlgill ghostscript`
Run Code Online (Sandbox Code Playgroud)
我使用了3 种方法来安装 GhostScript,但仍然出现相同的错误。为什么会这样,我还能如何解决这个问题?
仅供参考:以防万一你需要知道,我的操作系统是 Mac OS 10.11.2,我的 Python 版本是 3.5.1
编辑:所以,正如评论所说,尝试使用 Homebrew 解决这个问题。所以,我很自然地安装了它(因为我还没有安装 Homebrew)。然后,在成功安装后,我运行了brew install ghostscript. 在解决了一些问题之后,我能够成功安装 GhostScript(好吧,无论如何,根据 Homebrew 的说法)。然后我跑了brew doctor,发现它没有链接!****Gasp**** 嗯,修复很容易,只需链接即可!所以,这就是我所做的,在修复了其他一些问题之后,我终于能够成功执行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
Run Code Online (Sandbox Code Playgroud)
还是出了什么问题?为什么 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.
Run Code Online (Sandbox Code Playgroud)
但是我的保存功能仍然在 Python 中不起作用,现在我确定我已经安装了 GhostScript!每当我运行保存功能时,我仍然在 IDLE 中不断收到此错误:
FileNotFoundError: [Errno 2] No such file or directory: 'gs'
Run Code Online (Sandbox Code Playgroud)
还是出了什么问题?
编辑 #3: 这是我的 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.
Run Code Online (Sandbox Code Playgroud)
也许这就是导致我的问题的原因?如果是这样,我将如何修复它?
编辑#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'
Run Code Online (Sandbox Code Playgroud)
这真的很奇怪,因为gs在终端中工作并且这个:
x=subprocess.Popen(['/usr/local/bin/gs', '--version'], stdout=subprocess.PIPE)
print(x.stdout.read())
Run Code Online (Sandbox Code Playgroud)
在 Python IDLE 中工作。但出于某种原因,Python 成像库 (PIL) 仍然无法检测gs(可能是因为它的 subprocess.Popen() 函数?)。这是为什么?我的 Macintosh 上都安装了 xQuarts、GhostScript 和 PIL,但是当我运行我的savefirst()函数时仍然出现错误!为什么?
PS 这一切都发生在 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
Run Code Online (Sandbox Code Playgroud)
我通过运行pico ~MyName/.bash_profile然后输入来添加该路径:
PATH="Library/usr/local/bin/gs:${PATH}"
export PATH
Run Code Online (Sandbox Code Playgroud)
进入/.bash.profile文件。但是,我仍然FileNotFoundError在 Python 中不断得到它!我是否/usr/local/bin/gs以错误的方式附加到我的 PATH 中,甚至附加到错误的目录中?如果情况并非如此,那么为什么 Python Imaging Library 仍然找不到gs??
编辑#9:万岁!问题解决了!该错误不再出现。我所要做的就是/usr/local/bin/gs从我的 Mac 路径中删除,并且显然将它添加到我的 PYTHONPATH 中,就像在我的程序开始时一样:
os.environ["PATH"] += ":/usr/local/bin:/usr/local/bin/gs"
Run Code Online (Sandbox Code Playgroud)
哈扎!有用!PIL现在可以找到gs,并且可以将画布保存为JPEG,虽然它的分辨率很低,但这是另一个问题的问题。现在,我很高兴最初的问题得到了解决,我终于可以将画布另存为 JPEG(感谢@Copperfield!):)
只是一个想法:检查PIL的代码,它使用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'
>>>
Run Code Online (Sandbox Code Playgroud)
为了使此功能与您的代码一起使用,我做了以下更改:
#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
Run Code Online (Sandbox Code Playgroud)
并测试它工作正常。
在你的情况下,可能是:
#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
Run Code Online (Sandbox Code Playgroud)
是不行尝试其中之一
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"]
Run Code Online (Sandbox Code Playgroud)
编辑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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5392 次 |
| 最近记录: |