How to blur/pixelate part of an image using ImageMagick?

非 Y 不嫁゛ 提交于 2019-12-08 04:55:24

问题


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?


回答1:


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




回答2:


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.

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

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



来源:https://stackoverflow.com/questions/32332550/how-to-blur-pixelate-part-of-an-image-using-imagemagick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!