我正在使用 Perl 和 ImageMagick (Perl-API)。第一步,我获取图像的矩形并模糊图像的这一部分(加上该矩形的旋转)。 感谢 Mark Setchell,它的工作原理如下(请参阅此处的 stackoverflow 问题: How to blur/pixelate part of an image using ImageMagick? )。
现在我的目标是通过更大的像素来模糊该图像的矩形。 我怎样才能实现这一目标?
这是我迄今为止使用的 Mark Setchell 的代码:
#!/usr/bin/perl
use strict;
use warnings;
use Image::Magick;
my $x;
my $image;
my $blurred;
my $mask;
# Create original fishscale image
$image=Image::Magick->new(size=>'600x300');
$image->Read('pattern:fishscales');
$image->Write(filename=>"1.png");
# Copy original image and blur
$blurred = $image->Clone();
$blurred->GaussianBlur('x2');
$blurred->Write(filename=>"2.png");
# Make mask and rotate
$mask=Image::Magick->new(size=>'600x300');
$mask->Read('xc:white');
$mask->Draw(fill=>'black',primitive=>'rectangle',points=>'100,100,200,200');
$mask->Set('virtual-pixel'=>'white');
$mask->Rotate(20);
$mask->Transparent('white');
$mask->Write(filename=>"3.png");
# Copy mask as alpha channel into blurred image
$blurred->Composite(image=>$mask,qw(compose CopyOpacity gravity center));
# Composite blurred image onto original
$image->Composite(image=>$blurred);
$image->Write(filename=>'result.png');
更新:仍然存在一个问题:
它可以很好地处理选定的矩形,无需旋转。但如果我应用一个角度,我会得到奇怪的结果。像素化的区域不是我定义的区域,但当我选择远离图像中间的矩形和/或增加角度时,它会有所不同。
请参阅下面的图像,其中选择了不同的矩形以及像素化的区域。我使用的是 45 度角。
知道这里出了什么问题吗?(也许是“撰写 CopyOpacity 重心”)
最佳答案
类似这样的吗?
#!/usr/bin/perl
use strict;
use warnings;
use Image::Magick;
my $x;
my $image;
my $blurred;
my $mask;
# Create original fishscale image
$image=Image::Magick->new(size=>'600x300');
$image->Read('pattern:fishscales');
$image->Write(filename=>"1.png");
# Copy original image and scale
$blurred = $image->Clone();
$blurred->Resample(x=>'100',y=>'50',filter=>'point');
$blurred->Scale(width=>'1200',height=>'600');
$blurred->Write(filename=>"2.png");
# Make mask and rotate
$mask=Image::Magick->new(size=>'600x300');
$mask->Read('xc:white');
$mask->Draw(fill=>'black',primitive=>'rectangle',points=>'20,20,220,120');
$mask->Set('virtual-pixel'=>'white');
$mask->Rotate(20);
$mask->Transparent('white');
$mask->Write(filename=>"3.png");
# Copy mask as alpha channel into blurred image
$blurred->Composite(image=>$mask,qw(compose CopyOpacity gravity center));
# Composite blurred image onto original
$image->Composite(image=>$blurred);
$image->Write(filename=>'result.png');
关于image - 如何使用 ImageMagick 模糊/像素化图像的一部分,从而产生大像素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32351058/