gehho
gehho

Reputation: 9238

Blur part of an image only

I would like to blur only a part of an image. The part to blur is always rectangular, so that I can easily use the following command:

magick source.jpg -region 1000x1000+0+500 -blur 0x20 result.jpg

This works, but is pretty slow for large images. Since I have to process thousands of files again and again, this will simply take too long.

Therefore, I decided to do the blurring by downscaling and upscaling the image back to the original size. However, since this will blur the full image, I have tried to accomplish the task using the following steps:

  1. take the original image as background
  2. create a copy of the original image
  3. blur the copy using down-/upscaling
  4. crop the desired region from the blurred copy
  5. compose the original and the blurred&cropped copy

I am already pretty close (I hope), but when composing the two images, the cropped image will always be positioned in the top-left corner of the original image - instead of the original position from the source image. This is my current command:

magick source.jpg ( -clone 0 -resize 5% -resize 2000% -crop 1000x1000+0+1000 ) -composite result.jpg

I have read in the documentation that the original canvas size will be retained when using the -crop operation, and that this size and position will be used when using -composite. However, this doesn't seem to work in my case. Does anyone have an idea why?

I have tried to use -repage, -extent and other options to define the size and position of the cropped image, but to no avail so far.

Upvotes: 0

Views: 654

Answers (2)

Bonzo
Bonzo

Reputation: 5299

I would try -flatten in your command as that is used for layers.

Upvotes: 1

fmw42
fmw42

Reputation: 53154

You can do it with a mask image (of any shape) in ImageMagick. Though I am not sure if that will be faster than your scaling method.

Input:

enter image description here

Mask:

(note: blurring occurs where mask is black)

enter image description here

magick lena.jpg -write-mask mask.png -blur 0x3 +write-mask lena_blurred.png

Result:

enter image description here

Upvotes: 0

Related Questions