我已经在我的程序中实现了以下保存功能,允许用户将他/她在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')
但是,每当我运行这个save函数时,就会出现如下所示的错误:
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
编辑:如前所述,试着用自制的方法解决这个问题。所以,我很自然地安装了它(因为我还没有安装自制程序)然后,在成功安装之后,我运行了
brew install ghostscript
。在我解决了几个问题之后,我成功地安装了GhostScript(好吧,不管怎么说,根据自制程序)然后我运行brew doctor
发现它没有链接!****气喘吁吁,好吧,一个修复是很容易的,只要链接它所以,这就是我所做的,并且在修复了其他一些纠结之后,我终于成功地执行了brew link ghostscript
。然而,尽管我英勇地、顽强地试图改正错误,但它仍然保留着……是的,尽管我尽了最大的努力,当我运行save函数时仍然会出现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.
但是我的save函数在Python中仍然不起作用,现在我确信我已经安装了GhostScript每当我运行save函数时,仍会在idle中出现此错误:
FileNotFoundError: [Errno 2] No such file or directory: 'gs'
还有什么问题吗?
EDIT#3:Here是我的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 Imaging Library(PIL)仍然无法检测到
gs
(可能是因为它的subprocess.Popen()函数?)。为什么?我的Macintosh上安装了Xquarts、Ghostscript和PIL,但在运行savefirst()
函数时仍然会出错!为什么?另外,这一切都发生在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
文件。然而,我仍然在Python中得到FileNotFoundError
我是否以错误的方式将/usr/local/bin/gs
附加到我的路径中,甚至附加到错误的目录中?如果不是这样,那么为什么python映像库仍然找不到gs
??编辑9:欢呼问题解决了!错误不再出现我所要做的就是从mac路径中删除
/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
关于python - 即使在Macintosh上安装了GhostScript,Python <没有这样的文件或目录:'gs'>错误*问题仍然存在!*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34564145/