使用perl中的Inkscape shell
Inkscape有一个如下调用的shell模式使用perl中的Inkscape shell,perl,svg,automation,command-line-interface,inkscape,Perl,Svg,Automation,Command Line Interface,Inkscape,Inkscape有一个如下调用的shell模式 inkscape --shell 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 您可以在其中执行如下命令: some_svg_file.svg -e some_png_output.p
inkscape --shell
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
您可以在其中执行如下命令:
some_svg_file.svg -e some_png_output.png -y 1.0 -b #ffffff -D -d 150
/home/simone/some_text.svg -S
sub do_inkscape {
my ($file, $commands) = @_;
# capture output
return $output
}
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);
}
它将生成一个PNG文件,或类似于以下内容:
some_svg_file.svg -e some_png_output.png -y 1.0 -b #ffffff -D -d 150
/home/simone/some_text.svg -S
sub do_inkscape {
my ($file, $commands) = @_;
# capture output
return $output
}
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 --shell
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
我想这样做:
some_svg_file.svg -e some_png_output.png -y 1.0 -b #ffffff -D -d 150
/home/simone/some_text.svg -S
sub do_inkscape {
my ($file, $commands) = @_;
# capture output
return $output
}
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);
}
如果我像这样使用open2和forking,一切正常:
some_svg_file.svg -e some_png_output.png -y 1.0 -b #ffffff -D -d 150
/home/simone/some_text.svg -S
sub do_inkscape {
my ($file, $commands) = @_;
# capture output
return $output
}
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);
}
使用IPC::Open2;
$pid=open2(\*CHLD\u OUT,\*CHLD\u IN,'inkscape--shell');
$\=“\n”;$/=">";
我的美元用完了;打开我的$fh,'>',\$out;
如果(!已定义($kidpid=fork()){
骰子“不能叉:$!”;
}elsif($kidpid==0){
while(){print CHLD_IN$\u;}
}否则{
while(){chop;s/\s*$//gmi;打印“\”$\u\”;}
waitpid($kidpid,0);
}
但我不知道如何只输入一行,并且只捕获输出,而不必每次重新启动Inkscape
谢谢
Simone您不需要fork,
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\u till\u提示符
从inkscape
s输出读取,直到找到一个
字符,并假设当它看到一个字符时,inkscape
已准备就绪
注意:这太简单了,如果一个
可以出现在您所期望的输出中的提示符之外,您可能需要更多的逻辑才能使它工作得更可靠。在上面的脚本中也没有任何错误检查,这是错误的