Preparing images for page layout

in

Note: This script may not be production stable. You’re welcome to test it and use it, but don’t rely on it to work. Also, please read about some known issues towards the bottom of this page.

Introduction: boring task are for machines

If you’re working with the graphic production of magazines and other printed material on a regular basis, you’re probably familiar with the problem of preparing large numbers of images for placing into your layouts. They often arrive in a variety of formats and resolutions which makes it difficult to know how large they can be scaled in the layout documents without the resolution becoming too low. You have to make sure each image has the right resolution and is saved in an appropriate file format before placing them in your documents. Surely, there has to be a better way than opening up and resaving each image in Photoshop.

This is a description of how you can automate the process with a shell script that takes a bunch of images and converts them all to a file format and resolution of your preference. All of this is done with ImageMagick, a Unix/Linux program which one might call the command-line version of Photoshop.

The script is smart enough to take a whole directory structure with images and maintain that structure while converting the images. This is something that tools like Photoshop’s Actions are incapable of. It can be run completely in the background, without each image popping up on the screen like in Photoshop. Also, a shell script can be installed and run automatically on a server, maybe on the a file server where you an your co-workers keep your work files, thus avoiding using your own comuter’s resources for this job.

What the script does

This script checks an “In” directory for images, processes them and puts them in an “Out” directory. It sets the resolution to 300 ppi (I prefer this term over “dpi” in this case since we’re talking about *p*ixels here – not physical *d*ots) and saves the images in tiff format with lossless LZW compression. These options are possible to change though.

Typically, you would put a directory with images into the “In” directory and after a short while that same directory will reappear in “Out”, but with the images converted. You can even drop multi-level directory structures into “In” – the script recursively processes each directory within it’s parent directory.

Just to be safe, the script will save your original images in a ”_originals” directory. You can delete this directory once you’ve confirmed that the images are properly converted.

Requirements

The script is written in bash and uses ImageMagick to do the actual image conversions. It will probably run on any flavor of Unix/Linux (including Mac OS X) with small modifications. You just have to make sure you have the package “imagemagick” installed (bash should already be installed). On Mac OS X you can use the packaging system Fink to install ImageMagick as well as a lot of other Unix software.

The script

Time for some real work. This is the entire script:

#!/bin/bash


#- - - Configuration - - -

#Directory paths may not contain spaces! (No?)

#This is likely the only option that _has_ to be changed
IMG_DIR="/home/user/tiff_300"

IMG_IN="$IMG_DIR/in"

IMG_OUT="$IMG_DIR/ut"
IMG_TMP="$IMG_DIR/_tmp"

#Subdirectories where untouched and original images are stored
IMG_UNTOUCHED="_untouched"
IMG_ORIGINALS="_originals"

CONVERT_EXECUTABLE="/usr/bin/convert"

CONVERT_OPTIONS="-density 300x300 -compress LZW"

#Files with these suffixes are converted, others are left untouched
IMG_SUFFIXES="jpg
jpeg
JPG
JPEG
tif
tiff
TIF
TIFF
gif
GIF
png
PNG
bmp
BMP"


#- - - Helper functions - - - 

cleanup_and_exit() {
        if [ -e "$IMG_TMP" ]
        then
                mv $IMG_TMP/* $IMG_OUT/
                rm -r "$IMG_TMP"
        fi
        exit $1
}

mkdir_or_exit() {
        if [ ! -d "$1" ]
        then
                mkdir "$1"

        fi
        if [ ! -d "$1" ]
        then
                cleanup_and_exit 1
        fi
}


#- - - Create directories - - -

for DIR in "$IMG_DIR" "$IMG_IN" "$IMG_OUT"
do
        mkdir_or_exit "$DIR"
done


#- - - Check if the $IMG_IN directory is empty - - -

if [[ -z $(ls "$IMG_IN") ]]
then
        cleanup_and_exit 0
fi


#- - - Check if the $IMG_DIR directory is being modified - - -

sample_one=`du -s $IMG_DIR`
sleep 1
sample_two=`du -s $IMG_DIR`

if [[ $sample_one != $sample_two ]]
then
        #Files are being copied or moved, abort
        cleanup_and_exit 1
fi


#- - - Main conversion function - - -

convert_directory() {

        local file
        local file_suff
        local file_base
        local file_is_image


        #Check if the directory is empty
        if [[ -z $(ls "$1") ]]
        then
                return 0
        fi

        cd "$1"


        #Walk through directory contents
        for file in *
        do

                #If for some reason $IMG_UNTOUCHED already exists, don't try to convert it's contents
                if [[ "$file" <1> "$suff" ]
                        then
                                file_is_image="1"
                        fi
                        done

                if [ $file_is_image ]
                then
                        $CONVERT_EXECUTABLE $CONVERT_OPTIONS "$file" "$file_base.temp.tif"

                        if [ -e "$file_base.temp.tif" ]
                        then
                                mkdir_or_exit "$IMG_ORIGINALS"
                                mv "$file" "$IMG_ORIGINALS"
                                mv "$file_base.temp.tif" "$file_base.tif"

                        else
                                mkdir_or_exit "$IMG_UNTOUCHED"
                                mv "$file" "$IMG_UNTOUCHED"
                        fi
                else
                        mkdir_or_exit "$IMG_UNTOUCHED"
                        mv "$file" "$IMG_UNTOUCHED"

                fi

        done

}


#- - - Run conversion - - -

mkdir_or_exit "$IMG_TMP"
mv $IMG_IN/* $IMG_TMP/

convert_directory "$IMG_TMP"


#- - - Cleanup - - -

cleanup_and_exit 0

Check the code comments for some sort of explanation of how it all works. In the beginning of the script there are a few configuration options that you have to adjust – at least the IMG_DIR variable. If you installed Imagemagick with Fink on Mac OS X, you have to change CONVERT_EXECUTABLE to ”/sw/bin/convert”.

Installing

Save the script to a file, for example “tiff_300”. Set execute permissions on the file. Now you should be able to test it by simply running it from the command line.

If you’re unconfortable with running command line programs regularly you may want to run the script automatically in the background. You can use cron to have it run every minute (but please note that any error messages that the script generates may flood your mailbox):


#minute	hour	mday	month	wday	command
*	*	*	*	*	/path/to/tiff_300 > /dev/null

...and that’s it. (Obviously, you have to write the correct path.)

Pleace drop a line using the form below if you have any questions or comments. Good luck!

Known problems and limitations

  • Unfortunately, images saved by Photoshop have the resolution info saved in a non-standard format. Even if Imagemagick sets a new resolution for these images, Photoshop’s resolution is still left in the metadata and that info is read by many graphic programs. This means that the script is best used with images that have not been saved by Photoshop. A solution to this problem would be very welcome!
  • Imagemagick sometimes gives warnings about unrecognized metadata in the images. This is often information added by Photoshop that may be a custom extension of the specifications of the image format being used.
  • The script should probably accept command line arguments instead of having the settings hard coded in the script, or at least provide the possibility to have command line options override the settings.

Requested features

I’d like to be able to right-click on a folder on my Mac and choose ‘Convert contents to tiff 300’. Anyone who has any ideas on how to connect the script to a contextual menu like that? I guess that Folder Actions, the Scripting Menu and other AppleScript features could be used as well, and very easily – I’m just not that familiar with that stuff. Please let me know if you have any useful information.

Comments

Hi not sure if you have got round the Photoshop resolution problem. But I found this works.

C:\>mogrify -strip original.jpg
(-strip removes the photoshop resolution)

C:\>convert original.jpg -density 300x300 300dpi.jpg

Thanks, Dave W

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h2>
  • Lines and paragraphs break automatically.

More information about formatting options

Byt till svenska