imageperlimagemagickblurperlmagick

How to blur/pixelate part of an image using ImageMagick resulting in big pixels?


I am using Perl and ImageMagick (Perl-API). In a first step i take a rectangle of an image and blur this part of the image (plus rotation of that rectangle). Thanks to Mark Setchell this works as further bellow (see stackoverflow question here: How to blur/pixelate part of an image using ImageMagick?).

Now my aim is to blur the rectangle of that image with the result of bigger pixels. How can i achieve that?

Here the code of Mark Setchell that i use so far:

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

UPDATE: Still one problem remains:

It works fine with a selected rectangle without rotation. But if i apply an angle i get wierd results. The area that gets pixelated is not the area i have defined but it differs the more i select the rectangle away from the middle of the image and/or increase the angle.

See the images below with different rectangles selected and the areas that gets pixelated. I have used an angle of 45 degree.

enter image description here

enter image description here

enter image description here

Any idea what the problem is here? (maybe 'compose CopyOpacity gravity center')


Solution

  • Something like this?

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