Batch blur images in folder

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
RossTP
Posts: 2
Joined: 2016-08-16T03:39:52-07:00
Authentication code: 1151

Batch blur images in folder

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Batch blur images in folder

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Batch blur images in folder

Post 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
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Batch blur images in folder

Post 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.)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Batch blur images in folder

Post 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
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Batch blur images in folder

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Batch blur images in folder

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Batch blur images in folder

Post 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...
RossTP
Posts: 2
Joined: 2016-08-16T03:39:52-07:00
Authentication code: 1151

Re: Batch blur images in folder

Post 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 :)
Post Reply