user2028856
user2028856

Reputation: 3183

Image Magick blur part of image with mask

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!

Upvotes: 0

Views: 797

Answers (1)

fmw42
fmw42

Reputation: 53154

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

https://imagemagick.org/Usage/compose/#compose https://imagemagick.org/Usage/resize/ https://imagemagick.org/Usage/masking/#write_mask

ADDITION

If you want to feather the edges, you need to blur the mask as well.

Slow:

time magick barns_grand_tetons.jpg \
\( +clone -blur 0x60 \) \
\( barns_grand_tetons_mask.png -blur 0x10 -level 0x50% \) \
-compose over -composite slow_blur.jpg
 5.183s

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%.

Upvotes: 1

Related Questions