gpt4 book ai didi

python - 将 Ghostscript 从 os.popen 转换为 subsession.popen python

转载 作者:行者123 更新时间:2023-11-28 17:49:08 29 4
gpt4 key购买 nike

我需要为 Ghostscript 创建一个 Monkey 补丁,我必须从 os.popen 迁移到 subsession.popen 因为我不能在我的系统中使用 shell .

我这样试过:

def mioGhostscript(tile, size, fp):
"""Render an image using Ghostscript (Unix only)"""

# Unpack decoder tile
decoder, tile, offset, data = tile[0]
length, bbox = data

import tempfile, os

file = tempfile.mktemp()

# Build ghostscript command
command = ["gs",
"-q", # quite mode
"-g%dx%d" % size, # set output geometry (pixels)
"-dNOPAUSE -dSAFER", # don't pause between pages, safe mode
"-sDEVICE=ppmraw", # ppm driver
"-sOutputFile=%s" % file,# output file
"- >/dev/null 2>/dev/null"
]

#command = shlex.split(string.join(command))
# push data through ghostscript

try:
#gs = os.popen(command, "w")

args = command#['gs','-dSAFER','-dNOPAUSE','-dBATCH','-sDEVICE=jpeg','-sOutputFile=/home/user/output2.jpg /home/user/downloads/test.pdf']
gs = subprocess.Popen( args, stdout = PIPE, stderr = STDOUT, stdin=PIPE )
# adjust for image origin
if bbox[0] != 0 or bbox[1] != 0:
#gs.write("%d %d translate\n" % (-bbox[0], -bbox[1]))
gs.stdin.write("%d %d translate\n" % (-bbox[0], -bbox[1]))
fp.seek(offset)
while length > 0:
s = fp.read(8192)
if not s:
break
length = length - len(s)
raise Exception(s)
gs.stdin.write(s)
gs.communicate()[0]
status = gs.stdin.close()
#status = gs.close()
#if status:
# raise IOError("gs failed (status %d)" % status)
im = Image.core.open_ppm(file)
finally:
try: os.unlink(file)
except: pass

return im

import PIL
PIL.EpsImagePlugin.Ghostscript = mioGhostscript

但我有这个回溯:

Traceback (most recent call last): File "/home/web/lib/driver_mod_python.py", line 252, in handler buf = m.__dict__[pard['program']](pard) File "/home/dtwebsite/bin/cms_gest_ordini.py", line 44, in wrapped return func(pard) File "/home/dtwebsite/bin/cms_gest_ordini.py", line 95, in wrapped return func(pard) File "/home/dtwebsite/bin/cms_gest_picking_list.py", line 341, in picking_list tr_modelllo = render_row_picking_list(pard, item, picked=0, plist_allowed=plist_allowed) File "/home/dtwebsite/bin/cms_gest_picking_list.py", line 432, in render_row_picking_list aa = a.tostring() File "/rnd/apps/interpreters/python-2.5.6/lib/python2.5/site-packages/PIL/Image.py", line 532, in tostring self.load() File "/rnd/apps/interpreters/python-2.5.6/lib/python2.5/site-packages/PIL/EpsImagePlugin.py", line 283, in load self.im = Ghostscript(self.tile, self.size, self.fp) File "/home/dtwebsite/bin/cms_gest_picking_list.py", line 64, in mioGhostscript gs.stdin.write(s) IOError: [Errno 32] Broken pipe 

有人可以帮帮我吗?

最佳答案

我找到了问题的解决方案。它与 PIL 包有关,在安装过程中有些东西没有正确编译。之后我遇到了依赖问题。我通过以下方式修复了它:

import PIL.EpsImagePlugin
PIL.EpsImagePlugin.Ghostscript = mioGhostscript

然后我在命令中看到了这个:

"- >/dev/null 2>/dev/null"

该代码是 shell 的代码,它在我的系统上不起作用,因为 python 试图读取一个字面上名为 ->/dev/null 2>/dev/null 的文件,但它没有不存在。

我换了

"- >/dev/null 2>/dev/null"

"-" 

程序现在从 stdin 中读取。

最终代码为:

def mioGhostscript(tile, size, fp):
"""Render an image using Ghostscript (Unix only)"""

# Unpack decoder tile
decoder, tile, offset, data = tile[0]
length, bbox = data

import tempfile, os

file = tempfile.mktemp()

# Build ghostscript command
command = ["gs",
"-q", # quite mode
"-g%dx%d" % size, # set output geometry (pixels)
"-dNOPAUSE -dSAFER", # don't pause between pages, safe mode
"-sDEVICE=ppmraw", # ppm driver
"-sOutputFile=%s" % file,# output file
"-"
]

#command = shlex.split(string.join(command))
# push data through ghostscript

try:
#gs = os.popen(command, "w")

args = command#['gs','-dSAFER','-dNOPAUSE','-dBATCH','-sDEVICE=jpeg','-sOutputFile=/home/user/output2.jpg /home/user/downloads/test.pdf']
gs = subprocess.Popen( args, stdout = PIPE, stderr = STDOUT, stdin=PIPE )
# adjust for image origin
if bbox[0] != 0 or bbox[1] != 0:
#gs.write("%d %d translate\n" % (-bbox[0], -bbox[1]))
gs.stdin.write("%d %d translate\n" % (-bbox[0], -bbox[1]))
fp.seek(offset)
while length > 0:
s = fp.read(8192)
if not s:
break
length = length - len(s)
gs.stdin.write(s)
gs.communicate()[0]
status = gs.stdin.close()
#status = gs.close()
#if status:
# raise IOError("gs failed (status %d)" % status)
im = Image.core.open_ppm(file)
finally:
try: os.unlink(file)
except: pass

return im

import PIL.EpsImagePlugin
PIL.EpsImagePlugin.Ghostscript = mioGhostscript

我希望这篇文章可以帮助到一些人。

关于python - 将 Ghostscript 从 os.popen 转换为 subsession.popen python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13508055/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com