简体   繁体   English

如何在PerlMagick中设置字幕?

[英]How can I center captions in PerlMagick?

Using ImageMagick on the command line I can say 我可以说在命令行上使用ImageMagick

convert -background '#0000' -fill white -stroke black -strokewidth 3 -gravity center -pointsize 78 -size 568x1000 caption:'Lorem ipsum etc etc' -trim +repage out.png

And produce the output I'm looking for. 并产生我正在寻找的输出。 What I'd like to do is the same thing but within PerlMagick so that I don't have to keep reading and writing files as I perform various other steps. 我想做的是同样的事情,但在PerlMagick中,因此我不必在执行各种其他步骤时继续阅读和编写文件。 Here's what I have so far 这是我到目前为止所拥有的

use strict;
use warnings;
use Image::Magick;

my $im = new Image::Magick;
my $e = $im->Set(
        background => '#0000',
        fill => 'white',
        stroke => 'black',
        strokewidth => 3,
        gravity => 'center',
        pointsize => 78,
        size => '586x1000',
);
die $e if $e;

$e = $im->Read("caption:Lorem ipsum etc etc");
die $e if $e;

$e = $im->Trim();
die $e if $e;

$e = $im->Set(page=>'0x0+0+0'); # +repage
die $e if $e;

$e = $im->Write('out.png');
die $e if $e;

And this works precisely the same way, except that the resulting text is not centered. 除了生成的文本不居中之外,这种方式的工作方式完全相同。

Documentation on PerlMagick is almost nonexistent. 关于PerlMagick的文档几乎不存在。 I based this "read caption" syntax on some MagicWand examples, where it is claimed that this will result in centered text. 我在一些MagicWand示例中使用了这个“读取标题”语法,声称这将导致居中文本。 Clearly something is different for PerlMagick. 显然PerlMagick有些不同。

So, the question: How can I make PerlMagick respect gravity in this case? 所以,问题是:在这种情况下,我如何让PerlMagick尊重引力? How do I get multi-line, centered and word-wrapped text via PerlMagick? 如何通过PerlMagick获得多行,居中和自动换行的文本? Note that this requires that I use caption and not annotate or draw. 请注意,这需要我使用标题而不是注释或绘制。 I'd prefer to avoid manual per-line centering, but I would consider it. 我宁愿避免手动的每行中心,但我会考虑它。

Alternatively, if someone has a sample of doing word wrapping and with proportional fonts and Annotate then that would work for me. 或者,如果有人做了自动换行和比例字体和Annotate的样本,那么这对我有用。

EDIT: Please note that the -caption option to polaroid, though it shares implementation with what I'm doing, is not the same as the caption: pseudo-image. 编辑:请注意,宝丽来的-caption选项虽然与我正在做的事情共享实现,但与标题:伪图像不同。 I would still accept an answer using polaroid and -caption if the output closely matches what is given by the example convert command above. 如果输出与上面的示例转换命令给出的输出非常匹配,我仍然会接受使用polaroid和-caption的答案。

EDIT 2: Here's a more minimal example of the problem. 编辑2:这是一个问题的最小例子。

use strict;
use warnings;
use Image::Magick;

my $im = new Image::Magick;
my $e = $im->SetAttribute(
        background => '#0000',
        pointsize=>12,
        size => '100x100',
        gravity => 'center',
);
die $e if $e;
$e = $im->ReadImage('caption:The quick brown fox jumps over the lazy dog.');
die $e if $e;
$e = $im->Write('out.png');
die $e if $e;

Expected result: The text is centered. 预期结果:文本居中。

Actual result: The text is left-justified. 实际结果:文本左对齐。

Actual result should be identical to the output of this command: 实际结果应与此命令的输出相同:

convert -background '#0000' -size 100x100  -pointsize 12 -gravity center caption:'The quick brown fox jumps over the lazy dog.' out.png

From looking at the perlmagick source I see nothing that should be intercepting a particular SetAttribute call, so I don't see why gravity is being ignored. 从查看perlmagick源代码,我看不出任何应该拦截特定SetAttribute调用的东西,所以我不明白为什么重力被忽略。 How can I get gravity to not be ignored for this? 为什么我不能忽视引力? Or, how else can I do word wrapped and centered text with this kind of output? 或者,我怎么能用这种输出做单词包装和居中文本?

How about using Annotate()? 如何使用Annotate()?

use strict;
use warnings;
use Image::Magick;

my $im = Image::Magick->new();
$im->Set(size => "1000x568");
$im->ReadImage('xc:black');
$im->Annotate(text => "Lorem ipsum etc etc",
              gravity => "Center",
              fill => 'white',
              stroke => 'black',
              strokewidth => 3,
              pointsize => 78);
$im->Write('myout.png');

替代文字

Version: ImageMagick 6.5.7-8 版本:ImageMagick 6.5.7-8

Closer but still does not center horizontally, just vertically. 更接近但仍然不是水平居中,只是垂直居中。 Kinda running out of ideas... 有点想法......

#!/usr/bin/perl

use strict;
use warnings;
use Image::Magick;

my $t = Image::Magick->new();
my $b = Image::Magick->new();
$t->SetAttribute(pointsize=>12, size => '100x50', background=>'transparent');
$b->SetAttribute(size => '100x100');
$t->ReadImage('caption:The quick brown fox jumps over the lazy dog.');
$b->ReadImage('xc:transparent');
$b->Composite(image => $t, gravity=>'center', compose=>'over');
$b->Write('out.png');

替代文字

This works on Ubuntu 10.04 for me. 这对我来说适用于Ubuntu 10.04。 I only see documentation for "caption" working with the "polaroid" effect. 我只看到使用“宝丽来”效果的“标题”文档。

#!/usr/bin/perl

use strict;
use warnings;
use Image::Magick;

my $im = new Image::Magick;
$im->Set(size => '586x1000');

my $e = $im->ReadImage('xc:black');

$im->Polaroid(
        fill => 'white',
        stroke => 'black',
        strokewidth => 3,
        gravity => 'center',
        pointsize => 78,
        caption => "Lorem ipsum etc etc"
);

$e = $im->Trim();
die $e if $e;

$e = $im->Set(page=>'0x0+0+0'); # +repage
die $e if $e;

$e = $im->Write('out.png');
die $e if $e;

替代文字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM