Reputation: 50832
I am using Perl and ImageMagick (Perl-API). In a first step i would like to take a rectangle of an image and blur this part of the image. Desired result is the original image with the rectangle blured.
In a second step i need to blur a part of an image with a turned rectangle (i.e. turned by 35%).
How can i achieve this?
Upvotes: 1
Views: 3774
Reputation: 207670
As you asked for PerlMagick, I pulled my last few remaining hairs out to try and do this in Perl... the files 1.png
, 2.png
and 3.png
are purely for debug so you can see what I am doing.
#!/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');
Here are the debug images...
1.png
2.png
3.png
result.png
There may be a much faster, simpler, more efficient way of doing this, but I don't know it, and there are precious few examples of PerlMagick out there, so I'll stick my marker in the sand and see if anyone can better it:-)
P.S. Don't feel bad about my hair - there were only three left anyway :-)
Upvotes: 3
Reputation: 24439
Best way I can think of is to leverage masks to blur against. This would allow you to "draw" a shape, and pass through what to be blurred.
Example:
# Create base image
convert rose: -sample 200x rose_large.png
# Create mask
convert -size 200x131 xc:black -fill white -draw 'circle 100 65 100 25' rose_mask.png
# Blur with mask
convert rose_large.png -mask rose_mask.png -blur 0x8 +mask rose_blur_mask.png
Other techniques and examples here. I'm not familiar with Perl API, but there should be a Mask
method that accepts an image handler parameter.
For rectangle, you would simply update the shape to draw on the mask. Here's an example where I'm only blur-ing what's inside a rectangle.
# Create rectangle mask
convert -size 200x131 xc:white -fill black -draw 'rectangle 50 30 150 100' rose_rectangle_mask.png
# And repeat blur apply
convert rose_large.png -mask rose_rectangle_mask.png -blur 0x8 +mask rose_blur_retangle_mask.png
Upvotes: 4