在Windows上解决Ghostscript安装问题以配合Elaphe Python条形码生成器使用
我想用Elaphe来生成条形码。
但是当我运行示例代码时,出现了“gs失败”的错误。
我在一台64位的Windows电脑上工作。我安装了Python 2.7.1的win32版本,因为某些包需要win32。所以,我安装了Ghostscript 9.0.4的win32版本,并且把正确的路径添加到了PATH变量中。
我通过easy_install安装了elaphe、Python图像库和Python ghostscript 0.41的包(不确定这是否必要)。
我可以在Python中导入elaphe和PIL,并且执行命令没有错误。但是每当我尝试将条形码保存为图像时,就会出现错误。
当我运行这个简单的使用示例时:
>>> from elaphe import barcode
>>> barcode('qrcode',
... 'Hello Barcode Writer In Pure PostScript.',
... options=dict(version=9, eclevel='M'),
... margin=10, data_mode='8bits')) # Generates PIL.EpsImageFile instance
<PIL.EpsImagePlugin.EpsImageFile instance at ...>
>>> _.show() # Show the image
我得到的错误是 IOError: [Errno 32] Broken pipe
而当我运行这个来检查ghostscript和PIL的安装时:
# coding: utf-8
from StringIO import StringIO
from PIL.EpsImagePlugin import EpsImageFile
src = """%!PS-Adobe 2.0
%%BoundingBox: 0 0 144 144
36 36 72 72 rectfill
/Courier findfont 12 scalefont setfont
36 120 moveto (text) show
showpage
"""
im = EpsImageFile(StringIO(src))
im.save('foo.png')
我得到的错误是 IOError: gs failed (status 1)
我尝试安装Ghostscript的win64版本,但也出现了相同的错误。
有没有什么建议可以帮助我开始排查问题?我可以运行ghostscript的可执行文件,命令窗口会弹出来,而且我已经把正确的路径添加到了环境变量中。
谢谢。
2 个回答
1
我之前也遇到过同样的问题,后来按照以下步骤解决了:
- 安装PIL和Ghostscript。
- 访问这个链接:http://code.google.com/p/elaphe/issues/detail?id=7
- 滚动到页面底部,下载那个附加的文件(EpsImagePlugin.py)。
- 在C:\Python26\Lib\site-packages\PIL目录下备份当前的EpsImagePlugin.py文件。记得把"C:\Python26"替换成你自己Python的安装目录。
- 把下载的文件(EpsImagePlugin.py)放到C:\Python26\Lib\site-packages\PIL目录下。
1
你需要获取Ghostscript的输出,这样才能看到错误信息。通常这些信息会写到错误输出(stderr)里。我不知道在Python库里这些信息会去哪里,但你真的需要看到这些信息,才能弄清楚为什么Ghostscript会退出。
你还需要查看这些信息,以确定Ghostscript是否真的被执行了。听起来它可能没有被执行,但我对“python ghostscript 0.41 egg”这个东西也不太了解。