Technology, life, programming, travel and money.

Batch processing photos with DOS and ImageMagick

For my recent walking project I knew I was going to be taking lots of photos. I wanted a DOS script which would batch process the JPEG photos from my camera into the ‘full sized images’ (which are much smaller than the raw camera images), and the poloroid-like thumbnails.

The excellent ImageMagick tool provides a way to generate poloroid-like thumbnails automatically. I also wanted the thumbnails to be rotated at random angles which ImageMagick can do as well.

However the ImageMagick instructions were missing a few things 1) they seem to be aimed at people using Unix / Linux scripts whereas I’m using DOS scripts and 2) they show how to do this processing on single images rather than on a whole directory of them.

Poloroid rotation with ImageMagick Poloroid rotation with ImageMagick

It is quite easy to generate a random rotation angle with a simple bit of perl. The below line will generate a random floating point number between -7 and +7. I want both negative and positive numbers so that some of the poloroids will be tilted to the left, and others to the right.

perl -e "print rand() * 14 - 7"

I wanted to do all the processing through a DOS batch file so the next problem was how to get the random number into a DOS environmental variable. The answer is to use the ‘for /f‘ command to assign the output of a command into a variable:

for /f %%i in ('perl -e "print rand() * 14 - 7"') do set ANGLE=%%i

At the time when I wrote this script generating a poloroid involved a whole sequence of commands. Something like below!

-bordercolor white -border 6 -bordercolor grey60 -border 1 -background none -rotate %ANGLE% -background black ( +clone -shadow 60x4+4+4 ) +swap -background white -flatten

My script still uses this long poloroid command, but you might want to use the new one word ‘poloroid‘ command which is built straight into the latest versions of ImageMagick instead.

As well as generating the poloroid thumbnails it will also generate the full size website friendly images which are tagged with the website URL.

Prerequisites:
Your JPEG images are in an ‘images’ directory.

Usage:
ProcessPhotos.bat [path of directory that contains ‘images’ directory]

Output:
A new directory called ‘generated’ in the input directory containing a full size image (which has been tagged with the website URL), and a poloroid thumbnail.

Notes:
In order to use this script you will have to customise 1) the location of the ImageMagick executable 2) the reference to the drive – it is assuming a ‘f:’ drive here and 3) the website URL.

setlocal
pushd .
goto :skip_functions
:makeThumb
	echo %1
	set jpg=%1
	set jpg=%jpg:~0,-4%
	echo %jpg%
	for /f %%i in ('perl -e "print rand() * 14 - 7"') do set ANGLE=%%i
	set ANNOTATE=-gravity south -stroke "#000C" -strokewidth 2 -annotate +0+20 "www.advancedhtml.co.uk" -stroke none -fill white -annotate +0+20 "www.advancedhtml.co.uk"
	set POLOROID=-bordercolor white -border 6 -bordercolor grey60 -border 1 -background none -rotate %ANGLE% -background black ( +clone -shadow 60x4+4+4 ) +swap -background white -flatten
	D:appsImageMagick-6.3.6-Q16convert %1 ( +clone -resize 600x -auto-orient %ANNOTATE% -compress JPEG -quality 70 -write %jpg%_full.jpg +delete ) -resize 180x -auto-orient %POLOROID% -compress JPEG -quality 80 -sampling-factor 2x1 -strip %jpg%_th.jpg
goto :EOF
:skip_functions
f:
cd %1images
for /f %%i in ('dir /b *.jpg') do call :makeThumb %%i
md %1generated
move *_full.jpg ..generated
move *_th.jpg ..generated
popd
endlocal



Reader Feedback

2 Responses to “Batch processing photos with DOS and ImageMagick”

  1. Anthony says:

    I have just started looking at batch scripts and have just tried yours out; I was not sure what I was doing but I got there in the end.

    It works very well but I had to hard code an angle for now as I am unsure how to use perl – can you not generate a random number with the batch file ?
    I am not a great fan of the poleroid operator and your code generates a nicer image.

    A couple of comments:
    1/ I am working on my F drive as I didn’t want to mess up anything on my C drive if it went wrong. I had to use: poleroid f:Test_images If I didn’t add the f: to the path the images were saved in the same directory as the originals. and I had extra empty folders.
    2/ If you used -thumbnail instead of -resize you would not need the -strip.
    3/ Why didn’t you save the new images directly to the new folder rather than moving them later?
    4/ As I know nothing about batch scripts I could have done with a few more comments in the code. I have guessed what most of it means but have no idea about things like ” setlocal” and “pushd .”.

  2. Anthony says:

    I have made a couple of changes but have a problem and that is I can not get a negative random number. I was trying to do everything with the batch file.
    File saved as poleroid.bat on the F drive and the images are in a folder called Test_images. Usage = poleroid F:Test_images

    @echo off

    :: ?
    setlocal

    :: ?
    pushd .

    :: Jump to skip_functions
    goto :skip_functions

    :: function name makeThumbs
    :makeThumb

    :: Set the variable jpg to the first argument = generated
    set jpg=%1

    :: Remove the .jpg
    set jpg=%jpg:~0,-4%

    :: Add the path to the image in this case ” ..Test_imagesimage.jpg ”
    set jpg=..%DIRECTORY%%jpg%

    :: I am not sure how to get a negative angle
    set /a ANGLE=%random% %% 15 + 3

    :: Set the IM code for watermarking the image into the variable ANNOTATE
    set ANNOTATE=-gravity south -font Arial -pointsize 20 -fill black -annotate +0+20 “www.rubblewebs.co.uk” -fill white -annotate +2+22 “www.rubblewebs.co.uk”

    :: Set the IM code for the poleroid modification into the variable POLOROID
    set POLOROID=-bordercolor white -border 6 -bordercolor grey60 -border 1 -background none -rotate %ANGLE% -background black ( +clone -shadow 60×4+4+4 ) +swap -background white -flatten

    :: Run tha Image magick code
    convert %1 ( +clone -resize 600x -auto-orient %ANNOTATE% -compress JPEG -quality 70 -write %jpg%_full.jpg +delete ) -thumbnail 180x -auto-orient %POLOROID% -compress JPEG -quality 80 -sampling-factor 2×1 %jpg%_th.jpg

    :: Goes to the end of the function ?
    goto :EOF

    :: Function to create the directory to save the modified images into
    :skip_functions

    :: Select the drive ?
    f:

    :: select the folder ?
    cd %1images

    :: Create the new directory
    md %1generated

    :: variable for use in the thumbs function
    SET DIRECTORY=generated

    :: Call thefunction makethumb for all the images in the folder – added the folder path
    for /f %%i in (‘dir /b *.jpg’) do call :makeThumb %%i %DIRECTORY%

    :: ?
    popd

    :: ?
    endlocal

Leave a Reply

Do NOT fill this !