简体   繁体   English

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

[英]Why does Perl's Image::Magick create an outline around a semi-transparent image but composite doesn't?

I'm working on a program to dynamically add watermarks to product images with Image::Magick . 我正在开发一个程序,用Image :: Magick动态地为产品图像添加水印。 I'm using composite with the dissolve method. 我正在使用compositedissolve方法。 The watermark image is a PNG that has transparency. 水印图像是具有透明度的PNG。 It's running on Linux with ImageMagick 6.7.6-9 2012-05-16 Q16 . 它使用ImageMagick 6.7.6-9 2012-05-16 Q16在Linux上运行。

Consider the following arbitrary example images: 考虑以下任意示例图像:

Background ( test.jpg ): 背景( test.jpg ):

背景(产品图片)

Watermark/overlay ( example.png ): 水印/叠加( example.png ):

示例水印

If I put these together with the command line tool composite , everything is great. 如果我将这些与命令行工具composite一起放在一起,一切都很棒。

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

正确输出CLI

The text (this needs to be an image, there will be graphics in it, too) is superimposed over the background. 文本(这需要是一个图像,其中也会有图形)叠加在背景上。 The edges are just the way they were in the original watermark image. 边缘就像它们在原始水印图像中的方式。

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

Here's the output of this Perl program: 这是Perl程序的输出:

Perl程序的奇怪输出

As you can see, the new image has some strange edges, almost like an outline. 如您所见,新图像有一些奇怪的边缘,几乎像一个轮廓。 The larger this image gets (the original source images are both > 1000px) the more obvious this outline becomes. 此图像越大(原始源图像均> 1000px),此轮廓变得越明显。

Here's a closeup: 这是一个特写:

Closup正确的图像

不正确的图象特写镜头

I believe it might have something to do with the strength of the JPEG compression because the wrong image has a lot more artefacts. 我相信它可能与JPEG压缩的强度有关,因为错误的图像有更多的人工制品。 But that would mean the defaults of Perl's Image::Magick and the CLI are different. 但这意味着Perl的Image :: Magick和CLI的默认值不同。 I have not figured out how to set the compression yet. 我还没弄清楚如何设置压缩。

Anyway, I'd be glad about any kind of input on why this might happen, or ideas and suggestions on how to get rid of it. 无论如何,对于为什么会发生这种情况,或者如何摆脱它的想法和建议,我会很高兴。

I had a quick look at the PerlMagick source code and it seems that Composite with Dissolve is buggy when the dissolved image has an alpha channel. 我快速浏览了一下PerlMagick源代码,当溶解的图像有alpha通道时,似乎Composite with Dissolve是错误的。 The following works for me: 以下适用于我:

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

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

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

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