Home > database >  Image Magick blur part of image with mask
Image Magick blur part of image with mask

Time:08-21

I'm trying to blur the lower part of an image and repeat this for several hundred other images within a target folder. I'm using this command:

magick 1.jpg -write-mask mask.png -blur 0x60  write-mask 2.jpg

This does the the blurring but it takes between 10 - 20 seconds for one 5888 × 2944 image.

Is there someway to speed up this above command or is there a better command which will quickly batch blur a group of images?

Thanks!

CodePudding user response:

In Imagemagick, you can blur a region by blurring the whole image, then blend that with the original using mask. You can speed up a large blur by resizing (scaling) the image down, use a smaller blur and then resizing back up.

Input:

enter image description here

Mask:

enter image description here

Slow:

time magick barns_grand_tetons.jpg \
\(  clone -blur 0x60 \) \
barns_grand_tetons_mask.png \
-compose over -composite slow_blur.jpg
4.521s

enter image description here

Fast:

time magick barns_grand_tetons.jpg \
-set option:dims "%wx%h" \
\(  clone -scale 10% -blur 0x6 -scale "%[dims]" \) \
barns_grand_tetons_mask.png \
-compose over -composite fast_blur.jpg
0.368s

enter image description here

See

enter image description here

Fast:

time magick barns_grand_tetons.jpg \
-set option:dims "%wx%h" \
\(  clone -scale 10% -blur 0x6 -scale "%[dims]" \) \
\( barns_grand_tetons_mask.png -scale 20% -blur 0x2 -level 0x50% -scale "%[dims]" \) \
-compose over -composite fast_blur.jpg
0.404s

enter image description here

If you want to feather outside the mask rather than inside the mask (as above), then change -level 0x50% to -level 50x100%.

  • Related