如何在 PerlMagick 中将字幕居中?

发布于 2024-09-27 14:01:37 字数 2041 浏览 4 评论 0原文

在命令行上使用 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

并产生我正在寻找的输出。我想做的是同样的事情,但在 PerlMagick 中,这样我就不必在执行各种其他步骤时继续读取和写入文件。到目前为止,

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;

这就是我所得到的,其工作方式完全相同,只是生成的文本不居中。

PerlMagick 的文档几乎不存在。我将这种“阅读标题”语法基于一些 MagicWand 示例,据称这将导致文本居中。显然 PerlMagick 有所不同。

那么问题来了:在这种情况下我怎样才能让 PerlMagick 尊重重力呢?如何通过 PerlMagick 获得多行、居中和自动换行的文本?请注意,这要求我使用标题而不是注释或绘图。我宁愿避免手动每行居中,但我会考虑它。

或者,如果有人有一个自动换行、使用比例字体和注释的示例,那么这对我有用。

编辑:请注意,宝丽来的 -caption 选项虽然与我正在做的事情共享实现,但与标题不同:伪图像。如果输出与上面的示例转换命令给出的内容非常匹配,我仍然会接受使用宝丽来和 -caption 的答案。

编辑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;

预期结果:文本居中。

实际结果:文本左对齐。

实际结果应该与此命令的输出相同:

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

通过查看 perlmagick 源代码,我看不到任何应该拦截特定 SetAttribute 调用的内容,所以我不明白为什么重力被忽略。我怎样才能让重力不被忽视呢?或者,我还能如何使用这种输出来进行自动换行和居中文本?

Using ImageMagick on the command line I can say

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. 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. I based this "read caption" syntax on some MagicWand examples, where it is claimed that this will result in centered text. Clearly something is different for PerlMagick.

So, the question: How can I make PerlMagick respect gravity in this case? How do I get multi-line, centered and word-wrapped text via 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.

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. I would still accept an answer using polaroid and -caption if the output closely matches what is given by the example convert command above.

EDIT 2: Here's a more minimal example of the problem.

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. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我不是你的备胎 2024-10-04 14:01:37

使用 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');

替代文本

How about using 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');

alt text

影子是时光的心 2024-10-04 14:01:37

版本:ImageMagick 6.5.7-8

更接近,但仍然不水平居中,只是垂直居中。有点没主意了...

#!/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');

alt text

Version: 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');

alt text

走过海棠暮 2024-10-04 14:01:37

这对我来说适用于 Ubuntu 10.04。我只看到有关“标题”与“宝丽来”效果一起使用的文档。

#!/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;

替代文字

This works on Ubuntu 10.04 for me. 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;

alt text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文