如何使用ImageMagick模糊/像素化图像的一部分导致大像素?

时间:2015-09-02 10:45:23

标签: image perl imagemagick blur perlmagick

我正在使用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度的角度。

enter image description here

enter image description here

enter image description here

知道问题在这里吗?(也许'撰写CopyOpacity重心')

2 个答案:

答案 0 :(得分:3)

这样的东西?

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

enter image description here

答案 1 :(得分:1)

我会接受Mark Setchells的回答,但在此处将我的解决方案发布到更新的问题。我使用辅助函数,我只修改了"制作蒙版并旋转" -part。

# helperfunction
sub listDraw
{
    my $image = shift;
    my $result = undef;

    for my $attrs (@_)
    {
        my ($left, $top, $width, $height, $angle) = delete @$attrs{qw(left top width height angle)};

        if ($angle - int($angle/180)*180)
        {
            my ($xm, $ym, $rad) = ($left + $width/2, $top + $height/2, $angle * PI/180);
            $attrs->{primitive} ||= 'Polygon';
            $attrs->{points} = join ' ', map {($xm + ($_->{x}-$xm)*cos($rad) + ($_->{y}-$ym)*sin($rad)) . ',' . ($ym - ($_->{x}-$xm)*sin($rad) + ($_->{y}-$ym)*cos($rad))}
                ({ x => $left, y => $top }, { x => $left + $width, y => $top }, { x => $left + $width, y => $top + $height }, { x => $left, y => $top + $height });
        }
        else
        {
            $attrs->{primitive} ||= 'Rectangle';
            $attrs->{points} ||= $left . ',' . $top . ' ' . ($left + $width - 1) . ',' . ($top + $height - 1);
        }
        last if $result = $image->Draw(%$attrs);
    }
    return $result;
}

    # Make mask and rotate
    $mask=Image::Magick->new(size=>'600x300');
    $mask->Read('xc:white');

    $mask = listDraw($mask, {
            left        => $selection_y, # y of top left corner
            top         => $selection_x, # x of top left corner
            width       => $selection_width,
            height      => $selection_height,
            angle       => 180 - $angle,
            fill        => 'black',
    });
    $mask->Set('virtual-pixel' => 'white');
    $mask->Transparent('white');