Inkscape有一个shell模式调用,如下所示
inkscape --shell您可以在其中执行如下命令:
some_svg_file.svg -e some_png_output.png -y 1.0 -b #ffffff -D -d 150 它将生成一个PNG文件,或者如下所示:
/home/simone/some_text.svg -S它给出了返回消息中文件中所有元素的边界框,如下所示
svg2,0.72,-12.834,122.67281,12.942
layer1,0.72,-12.834,122.67281,12.942
text2985,0.72,-12.834,122.67281,12.942
tspan2987,0.72,-12.834,122.67281,12.942这样做的好处是,您可以对SVG文件执行操作,而不必每次都重新启动Inkscape。
我想做这样的事情:
sub do_inkscape {
my ($file, $commands) = @_;
# capture output
return $output
}如果我像这样使用open2和forking,事情就可以正常工作:
use IPC::Open2;
$pid = open2(\*CHLD_OUT, \*CHLD_IN, 'inkscape --shell');
$\ = "\n"; $/ = ">";
my $out; open my $fh, '>', \$out;
if (!defined($kidpid = fork())) {
die "cannot fork: $!";
} elsif ($kidpid == 0) {
while (<>) { print CHLD_IN $_; }
} else {
while (<CHLD_OUT>) { chop; s/\s*$//gmi; print "\"$_\""; }
waitpid($kidpid, 0);
}但我不知道如何只输入一行,并且只捕获该输出,而不必每次都重新启动Inkscape。
谢谢
西蒙尼
发布于 2011-10-08 16:47:55
你不需要派生,open2自己处理。您需要做的是找到一种方法来检测inkscape何时在等待输入。
下面是一个非常基本的例子,告诉你如何做到这一点:
#! /usr/bin/perl
use strict;
use warnings;
use IPC::Open2;
sub read_until_prompt($) {
my ($fh) = (@_);
my $done = 0;
while (!$done) {
my $in;
read($fh, $in, 1);
if ($in eq '>') {
$done = 1;
} else {
print $in;
}
}
}
my ($is_in, $is_out);
my $pid = open2($is_out, $is_in, 'inkscape --shell');
read_until_prompt($is_out);
print "ready\n";
print $is_in "test.svg -S\n";
read_until_prompt($is_out);
print $is_in "quit\n";
waitpid $pid, 0;
print "done!\n";read_until_prompt从inkscape的输出中读取,直到它找到一个>字符,并假定当它看到一个字符时,inkscape就准备好了。
注意:这太简单了,如果>可以出现在您期望的输出提示之外,那么您可能需要更多的逻辑来使它更可靠地工作。在上面的脚本中也没有错误检查,这是不好的。
https://stackoverflow.com/questions/7695641
复制相似问题