Archive for the ‘Images’ Category

Batch File Image Conversion

Friday, December 19th, 2008

So often I need to convert images from one file format to another or I need to create a number of derivatives, such as thumbnails, medium-sized images, large ones, etc.

There are a number of ways to do this, but my preference is to use ImageMagick, which is a suite of very powerful command line programs to view and manipulate images. It is available for Windows, Mac, and *nix (Linux in my case). I was first introduced to ImageMagick some years ago when I needed to install it as a dependency for another application I was installing at the time. Since then, I too have integrated it with a number of web applications and scripts to manipulate images as part of a image upload workflow. And, yes, you read correctly, it is a command line application.

This is the point where I plug batch scripting. Learn it (or in my case, relearn it) because it really is a handy way to create quick routines customized precisely to your needs.

I’ve included two batch files as examples, both of which use ImageMagick. One basically resizes and converts a bunch of images. The second is a specific image conversion batch file for creating images to be used with the Madison Digital Image Database, about which I say a little more below. Both of these scripts assume that the ImageMagick executable files are in your operating system’s path and both are design for MS-DOS, for use on Windows. The scripts are also variations on the same theme.

The first batch script is a straightforward resize and save. It takes all the TIF files in the given target directory and creates two derivative JPEGs, one medium-sized image and one thumbnail image.

@echo off

echo.
echo This file takes the target directory as an argument.
echo It will take all TIF files (*.tif) and create
echo image derivatives of two sizes: thumbnails and medium-sized images.
echo Those images will be saved into directories (thumbs, medium) within
echo the target directory.
echo.

REM Set thumbnail and medium-sized image sizes.
REM It will resize so that the longest side is not longer than these numbers (in pixels).
SET THUMBSIZE=150
SET MEDSIZE=300
       
SET CURRDIR=%CD%
SET TARGETDIR=%1

IF EXIST %TARGETDIR% GOTO CONVERT
GOTO NOTARGET

:CONVERT
        cd %TARGETDIR%
        IF EXIST thumbs RMDIR /s/q thumbs
        IF EXIST medium RMDIR /s/q medium
        mkdir thumbs
        mkdir medium
        dir *.tif /b > output.txt
        for /f "delims=" %%i in (output.txt) do convert "%%i" ( +clone -resize "%MEDSIZE%x%MEDSIZE%>" -unsharp "0" -write "medium/%%~ni_medium.jpg" +delete ) -resize "%THUMBSIZE%x%THUMBSIZE%>" -unsharp "0" "thumbs/%%~ni_thumb.jpg"
        del output.txt
        echo.
        echo Files converted.
        cd %CURRDIR%
        GOTO END

:NOTARGET
        echo.
        echo Target directory doesn’t exist.  Ending.
        GOTO END

:END
        echo.

The second batch script creates more tailored derivative images. Specifically, the images conform precisely to a set size, specifically those needed for the Madison Digital Image Database (MDID), which is a web application used in higher education that permits digital image curators to describe and store digital images and faculty to search for images, create presentations, and then show those presentations to students in the classroom as faculty once did by loading slides into slide projectors. When the image is resized, naturally, the script maintains the images proportions. But, if the image does not fill the area perfectly, the remaining white space is filled in with the color black.

@echo off

echo This file looks for a directory named originals.
echo It creates all the needed directories (postcards, thumbs)
echo It will replace any previously existing information.

SET USERDIR=%1%
IF NOT "
%USERDIR%"=="" chdir %USERDIR%

SET WORKINGDIR=%CD%

echo.
echo.
echo %WORKINGDIR%
echo.
echo.

IF EXIST thumbs RMDIR /s/q thumbs
IF EXIST postcards RMDIR /s/q postcards

SET THUMBSIZE=150
SET MEDSIZE=550
SET LARGESIZE=1000

SET EXTENSION=TIF

IF "%EXTENSION%"=="JPG" SET LCASEEXT=jpg
IF "
%EXTENSION%"=="jpg" SET LCASEEXT=jpg
IF "
%EXTENSION%"=="TIF" SET LCASEEXT=tif

mkdir thumbs
mkdir postcards

IF EXIST originals (
        echo Creating JPEG derivatives for MDID
       
        cd originals
        dir *.%EXTENSION% /b > output.txt
       
        echo Creating postcards…
        for /f "delims="
%%i in (output.txt) do convert -extent "320×240" xc:black -type "TrueColor" -colorspace "RGB" -gravity "center" "%%i" -resize "320×240" -composite -unsharp "0" "%WORKINGDIR%/postcards/%%~ni.jpg"

        echo.
       
        echo Creating thumbs…
        for /f "delims=" %%i in (output.txt) do convert -extent "96×72" xc:white -type "TrueColor" -colorspace "RGB" -gravity "center" "%%i" -resize "96×72" -composite -unsharp "0" "%WORKINGDIR%/thumbs/%%~ni.jpg"
       
        echo.
       
        del output.txt
        cd ..
)

:END
echo Done

Download the two batch files:
Convert Images Batch Scripts

Entries (RSS)