1

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?

Thariama
  • 50,002
  • 13
  • 138
  • 166
  • Unless you have many files that need processing in the an identical way you would be better off installing [GIMP](http://www.gimp.org/) -- the *GNU Image Manipulation Program* -- and doing it manually – Borodin Sep 01 '15 at 15:03
  • 1
    @Borodin: not an option, i need that functionality built in in our web-based CMS – Thariama Sep 02 '15 at 07:55

2 Answers2

4

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

rose_large.png

 # Create mask
 convert -size 200x131 xc:black -fill white -draw 'circle 100 65 100 25' rose_mask.png

rose_mask.png

 # Blur with mask
 convert rose_large.png -mask rose_mask.png -blur 0x8 +mask rose_blur_mask.png

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.

Update

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

rose_rectangle_mask

# And repeat blur apply
convert rose_large.png -mask rose_rectangle_mask.png -blur 0x8 +mask rose_blur_retangle_mask.png

rose_blur_retangle_mask.png

emcconville
  • 23,800
  • 4
  • 50
  • 66
3

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

enter image description here

2.png

enter image description here

3.png

enter image description here

result.png

enter image description here

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 :-)

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • plus 1 and answer accepted. Thank you very very much - that made my day. Btw. i do not have much hair left eighter :) – Thariama Sep 02 '15 at 10:38
  • do you know of a way to pixelate the whole thing with bigger pixels as results (above solution is perfect, but i just think it might be neat to have bigger blurred pixels instead of small blurred pixels)? – Thariama Sep 02 '15 at 10:40
  • for this i created a new stackoverflow question: http://stackoverflow.com/questions/32351058/how-to-blur-pixelate-part-of-an-image-using-imagemagick-resulting-in-big-pixels – Thariama Sep 02 '15 at 10:46