为什么Perl的Image :: Magick在半透明图像周围创建轮廓但复合图像不是?

时间:2013-01-28 12:50:36

标签: perl image-processing imagemagick

我正在开发一个程序,使用Image::Magick动态地为产品图片添加水印。我使用composite方法使用dissolve。水印图像是具有透明度的PNG。它使用ImageMagick 6.7.6-9 2012-05-16 Q16在Linux上运行。

考虑以下任意示例图像:

背景(test.jpg):

Background (product image)

水印/叠加(example.png):

Example watermark

如果我将这些与命令行工具composite放在一起,一切都很棒。

composite -dissolve 60% -gravity center example.png test.jpg out.jpg

Correct output of CLI

文本(这需要是一个图像,其中也会有图形)叠加在背景上。边缘就像它们在原始水印图像中的方式一样。

#!/usr/bin/perl
use strict; use warnings;
use Image::Magick;

# this objects represents the background
my $background = Image::Magick->new;
$background ->ReadImage( 'test.jpg' );

# this objects represents the watermark
my $watermark = Image::Magick->new;
$watermark->ReadImage( 'example.png');

# there is some scaling going on here...
# both images are scaled down to have the same width
# but the problem occurs even if I turn the scaling off

# superimpose the watermark
$background->Composite(
  image    => $watermark,
  compose  => 'Dissolve',
  opacity  => '60%',
  gravity  => 'Center',
);

$background->Write( filename => 'out.jpg' );

这是Perl程序的输出:

Weird output of Perl program

如您所见,新图像有一些奇怪的边缘,几乎像一个轮廓。此图像得到的图像越大(原始源图像都大于1000px),此轮廓变得越明显。

这是一个特写:

Closup of correct image

Closeup of incorrect image

我认为它可能与JPEG压缩的强度有关,因为错误的图像有更多的人工制品。但这意味着Perl的Image :: Magick和CLI的默认值不同。我还没弄清楚如何设置压缩。

无论如何,对于为什么会发生这种情况,或者有关如何摆脱它的想法和建议,我感到很高兴。

1 个答案:

答案 0 :(得分:1)

我快速浏览了PerlMagick源代码,当解散的图像具有alpha通道时,Composite Dissolve似乎是错误的。以下适用于我:

$watermark->Evaluate(
  operator => 'Multiply',
  value    => 0.6,
  channel  => 'Alpha',
);

$background->Composite(
  image    => $watermark,
  gravity  => 'Center',
);