在没有X服务器的情况下运行inkscape

问题描述:

我在* nix-like系统(OS X 10.6.8,各种风格的Linux)上从Gearman PHP进程运行inkscape,将SVG图像转换为PNG或PDF格式。我用的是这样的(添加这里只是为了清楚起见,换行符):在没有X服务器的情况下运行inkscape

/full/path/to/inkscape -z \ 
    --export-png=/path/to/output.png \ 
    --export-width=100 --export-height=100 \ 
    /path/to/input.svg 

它的工作原理,但尽管-z标志(“不使用X服务器”)我得到这个在我的控制台输出(OS X):

Setting Language: .UTF-8 

(process:44699): Gtk-WARNING **: Locale not supported by C library. 
    Using the fallback 'C' locale. 
Xlib: extension "RANDR" missing on display "/tmp/launch-WvcqRh/org.x:0". 

这表明,我认为Inkscape中加载多个库比实际需要的,而且它可能会更快,如果它不试图连接到X服务器。但是,除了使用-z/--without-gui标志之外,我不确定要尝试什么。在我的开发机器上的性能仍然是亚秒(至少对于简单的SVG文件),但如果可以的话,我想清除它。即使最好的答案只是“抑制错误输出”!

也许如果我关闭或重置一个bash DISPLAY env var?我对X完全不熟悉。

是的,如果你想让你的程序根本找不到X,你可以在unset DISPLAY之前启动该过程。

您还可以使用Xvfb来为 “假的” X服务器:http://en.wikipedia.org/wiki/Xvfb

你可能也想看看这些工具:

他们源代码是真的是小。

+0

谢谢,我会给'DISPLAY'的事情一去,并会报告回来!如果这样做不会产生结果,我会尝试使用Xvfb,但是我会以低的VPS运行这个系统,所以我想尽量减少额外的软件。 – halfer 2012-04-03 14:24:25

+0

Btw;我已经尝试过'svg2pdf',当然在我的Mac上,它似乎没有做好渲染Inkscape SVG文档的工作 - 来自单个家族的各种字体样式都被渲染为默认变体。 (也许我需要在这里配置一些东西,但是'inkscape'只是让它发挥作用)。 – halfer 2012-04-03 14:26:12

+0

我已经尝试过'DISPLAY =''inkscape --export-png = output.png input.svg'并且还有一个bash脚本,用'unset DISPLAY'和'inkscape'命令,既不抑制'Gtk-WARNING ”。像你一样,我宁愿期待那个工作 - b! – halfer 2012-04-06 17:19:37

(echo foo.ai --export-plain-svg foo.svg) | DISPLAY= inkscape --shell

+1

嗨,谢谢你。你能解释一下吗?我不是专家级的shell用户,但是我对'echo-'的'--export-plain-svg'参数感到困惑。你的技术的本质是图像数据通过一个空的'DISPLAY' env var被传送到'inkscape'命令? – halfer 2014-01-16 01:20:34

另一种方式来抑制输出,同时保留对真正的错误作出响应的能力,是从Python中调用Inkscape中。

import subprocess    # May want to use subprocess32 instead 

cmd_list = [ '/full/path/to/inkscape', '-z', 
      '--export-png', '/path/to/output.png', 
      '--export-width', 100, 
      '--export-height', 100, 
      '/path/to/input.svg' ] 

# Invoke the command. Divert output that normally goes to stdout or stderr. 
p = subprocess.Popen(cmd_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 

# Below, <out> and <err> are strings or <None>, derived from stdout and stderr. 
out, err = p.communicate()  # Waits for process to terminate 

# Maybe do something with stdout output that is in <out> 
# Maybe do something with stderr output that is in <err> 

if p.returncode: 
    raise Exception('Inkscape error: ' + (err or '?') ) 

在我的Mac OS系统中,这些混沌状态信息(由原始的海报所描述的)在err结束。此外,对于我跑某项工作,还有的是在out结束了额外的消息:(输入SVG文件通过339个像素的大小为339)

Background RRGGBBAA: ffffff00 
Area 0:0:339:339 exported to 100 x 100 pixels (72.4584 dpi) 
Bitmap saved as: /path/to/output.png