Blur the center of the image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
User avatar
shveicar
Posts: 42
Joined: 2019-10-15T15:10:16-07:00
Authentication code: 1152

Blur the center of the image

Post by shveicar »

Hello
Is there a way to blur a certain area in an image with Imagemagick?
For example this is the original image where you want to blur the center area 100x100

Image

In a graphics editor (such as Photoshop), this is quite easy to do with the Gauss blur filter

Image

I'm trying to create a similar effect with the command:

Code: Select all

convert input.png ( +clone 0 -blur 0x20 -resize 50% ) -gravity center -define compose:args=50 -compose dissolve -alpha on -composite output.png
but getting the usual darkened area..

Image

Can someone suggest a solution for this problem?
Thanks.
Windows, ImageMagick 7.0.8 version

snibgo
Posts: 12910
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Blur the center of the image

Post by snibgo »

This gives roughly the desired result:

Code: Select all

magick adb64cb06c5a075e8bed4ca6c8d4ef2e.png -region 125x125+63+63 -blur 0x3 +region blurredbricks.png
Image
snibgo's IM pages: im.snibgo.com

User avatar
shveicar
Posts: 42
Joined: 2019-10-15T15:10:16-07:00
Authentication code: 1152

Re: Blur the center of the image

Post by shveicar »

snibgo
Thank you very much.
And is there a way to do the same blur only with a given circular area?
That is:

Image
Windows, ImageMagick 7.0.8 version

snibgo
Posts: 12910
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Blur the center of the image

Post by snibgo »

"-region" can mask only rectangular regions. For more general masks, I suggest a masked composite (Windows BAT script):

Code: Select all

magick ^
  adb64cb06c5a075e8bed4ca6c8d4ef2e.png ^
  ( +clone ^
    -blur 0x3 ^
  ) ^
  ( +clone ^
    -fill Black -colorize 100 ^
    -fill White -draw "circle 125,125 125,188" ^
  ) ^
  -alpha off ^
  -compose Over -composite ^
  blurredbricks2.png
Image
Instead of "circle", draw whatever mask you want.
snibgo's IM pages: im.snibgo.com

User avatar
shveicar
Posts: 42
Joined: 2019-10-15T15:10:16-07:00
Authentication code: 1152

Re: Blur the center of the image

Post by shveicar »

Ok, it's interesting method.
Thank you again for your help.
Windows, ImageMagick 7.0.8 version

User avatar
GeeMack
Posts: 753
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Blur the center of the image

Post by GeeMack »

shveicar wrote:
2019-12-09T18:01:15-07:00
And is there a way to do the same blur only with a given circular area?
Very nearly the same as snibgo's method, but instead of using "-compose ... -composite", you can create a mask and use it with "-write-mask" to define the area that gets affected by the blur operation. Here's an example command that blurs a circular area in the center...

Code: Select all

magick adb64cb06c5a075e8bed4ca6c8d4ef2e.png -size %[w]x%[h] ^
   ( xc:white -draw "fill black circle 125,125 125,175" -write mpr:mask +delete ) ^
   -write-mask mpr:mask -blur 0x3 +write-mask result.png
You can make any sort of grayscale mask and use "-write-mask" to restrict the effect of many IM operations. This command creates a radial gradient mask instead of a solid circle, so the effect of the blur is feathered from the center outward...

Code: Select all

magick adb64cb06c5a075e8bed4ca6c8d4ef2e.png -size %[w]x%[h] ^
   ( radial-gradient: -negate -write mpr:mask +delete ) ^
   -write-mask mpr:mask -blur 0x3 +write-mask result.png

User avatar
shveicar
Posts: 42
Joined: 2019-10-15T15:10:16-07:00
Authentication code: 1152

Re: Blur the center of the image

Post by shveicar »

Thanks GeeMack but can I change the location for the "blurred circle" from the center to the upper right corner?
just like to have a choice of blur location in different parts of the image like how it does

Code: Select all

-gravity
option.
Windows, ImageMagick 7.0.8 version

User avatar
fmw42
Posts: 26273
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Blur the center of the image

Post by fmw42 »

Just put the center of the circle in the top right corner area. I do not think -gravity works with -draw.

For example (unix syntax)

Code: Select all

magick adb64cb06c5a075e8bed4ca6c8d4ef2e.png -size "%[w]x%[h]" \
\( xc:white -draw "fill black circle 200,50 200,100" -write mpr:mask +delete \) \
-write-mask mpr:mask -blur 0x3 +write-mask result.png
See drawing circles at https://imagemagick.org/Usage/draw/#circles

User avatar
shveicar
Posts: 42
Joined: 2019-10-15T15:10:16-07:00
Authentication code: 1152

Re: Blur the center of the image

Post by shveicar »

Thanks. now I understand how it works :)
Windows, ImageMagick 7.0.8 version

Post Reply