Page 1 of 1

Batch blur images in folder

Posted: 2016-08-16T03:50:27-07:00
by RossTP
Hi all,

I realise this might be posted somewhere, but I wasn't able to find it. I'm trying to add blur to the bottom section of 50,000 images. So far I've been able to achieve this for a single image (800 x 600) using the following code:

Code: Select all

convert -size 800x525 xc:black -size 100x75 xc:white -append  blur_mask.jpg
  convert /Users/Desktop/testFolder/image1.jpg blur_mask.jpg \
          -compose blur -define compose:args=3 -composite \
          blur.jpg
It works perfectly, but now I need to do this to all 50,000 images in the folder. How do I change the above code to achieve this?

Sample image can be downloaded here: https://dl.dropboxusercontent.com/u/290 ... image1.jpg

I'm very new to IM. I use a Macbook Pro running IM 7.0.1

I'd appreciate any assistance. Thanks in advance!

PS - I forgot to mention that I need the keep the existing filename for each modified image.

Re: Batch blur images in folder

Posted: 2016-08-16T04:42:02-07:00
by snibgo
I hope you have the copyright owner's permission to remove their copyright notice.

In bash, I would put it in a "for" loop, but I'm not a bash expert.
RossTP wrote:convert -size 800x525 xc:black -size 100x75 xc:white -append blur_mask.jpg
You create a pure black and white image, then save it as JPG which will change values to not quite white and not quite black. Eek. Use PNG instead.

Better still, do you need to keep blur_mask for some reason? If not, you can combine both converts into one, without saving the intermediate file.

Re: Batch blur images in folder

Posted: 2016-08-16T09:25:31-07:00
by fmw42
If you want to keep the same filename and do not want to overwrite your input image, then create a new directory for the output images

Assuming you only want JPGs and they are all the same size (for your blur image), then

Code: Select all

cd /Users/fred/desktop/test1
list=$(ls *.jpg)
for img in $list; do
	convert $img \
	\( -size 800x525 xc:black -size 800x75 xc:white -append \) \
	-compose blur -define compose:args=3 -composite \
	/Users/fred/desktop/test2/$img
done
cd
If your images are different sizes, then you will need to make your blur image a different way and use percentages.

See the use of parenthesis processing at http://www.imagemagick.org/Usage/basics/#parenthesis

Re: Batch blur images in folder

Posted: 2016-08-16T11:26:57-07:00
by GeeMack
fmw42 wrote:Assuming you only want JPGs and they are all the same size (for your blur image), then ...
If the images are all the same size and the blurred section is in the same location on all of them, the command inside the loop could be as simple as this...

Code: Select all

...
magick $img -gravity south -background none \
   \( +clone -extent 800x60 -extent 800x80 -blur 0x10 \) -composite /output_directory/$img
...
(I'm not on a *nix shell to test it, but I think I have the syntax correct.)

Re: Batch blur images in folder

Posted: 2016-08-16T11:38:02-07:00
by snibgo
We can also do this with "-region", eg:

Code: Select all

%IM%convert toes.png -gravity south -region 100%x30% -blur 0x3 b.png

Re: Batch blur images in folder

Posted: 2016-08-16T12:06:04-07:00
by GeeMack
snibgo wrote:We can also do this with "-region", eg:

Code: Select all

%IM%convert toes.png -gravity south -region 100%x30% -blur 0x3 b.png
For some reason I thought "-region" hadn't been implemented in IM7 yet, but trying that command with 7.0.2-9 on Windows 10 64 it seems to work perfectly.

I used the "-extent ... -extent ... -blur ... -composite ..." command to make a soft edge on the blurred section, but for pure simplicity, using "-region" like you have there seems like a better approach.

Re: Batch blur images in folder

Posted: 2016-08-16T12:47:25-07:00
by snibgo
On "-region" in v7, see viewtopic.php?f=1&t=29692&p=133296

It does what the OP asks for. But we often want a soft edge to effects.

Re: Batch blur images in folder

Posted: 2016-08-16T19:56:52-07:00
by GeeMack
Another way to repeat the blur operation on a particular location is to run something like this inside the "for" loop...

Code: Select all

...
magick -gravity south $img \
   \( +clone -crop 550x40+0+10 -background none -flatten -blur 0x6 \) -composite /output_directory/$img
...
That starts by telling the image and the following crop to obey "-gravity south". Next it crops out a copy of the specific area to be blurred with "-crop 550x40+0+10", in this case the geometry for the "image1.jpg" sample provided above. Then it uses the page information to flatten that cropped piece onto a transparent background with a viewport of the original image size. Now the blur can feather out past the cropped area onto the surrounding transparent canvas. Simply composite that layer back onto the original and save as...

Re: Batch blur images in folder

Posted: 2016-08-17T11:44:44-07:00
by RossTP
Thanks everyone for all your help! Really appreciate it. (FYI - these images are owned by myself, and it's actually the time/date stamp that we're trying to hide more than the copyright notice). Cheers :)