Creative Commons

2010-03-22 20:57:05

Automate DPI Altering With ImageMagick

One of the most mundane tasks I have to perform after a photo shoot is editing the dots per inch (DPI) of the almost-finished photos so that they print at the correct sizes (4x6, 5x7, etc.). This process usually involves me opening each photo, one at a time, and increasing the DPI until I see the print size reach the desired dimensions. For a couple photos, while mundane, this task isn't bothersome. However, when there are 96 photos to alter as in one of my recent shoots, this task can take forever.

In a Charles Epps moment, I realized that math could solve this problem. So the first thing I did was derive the equation I would need to determine the needed DPI of a photo:

dpi = width of the small side / 4

It was pretty simple, since 4 inches is the small side of a 4x6 photo, I would just have to divide the pixel dimension of the small side of the photo by 4 to get the necessary DPI. Now all I have to do is write some code to determine the pixel dimensions of a photo, do the math, and then apply the transformation. After I get the process down for one photo, I'd edit the script to do it for each image in a given directory automatically.

I ended up writing 4 scripts (4x6, 5x7, 8x10, and wallet) which need to be placed in the directory of photos and executed. Each script will leave the original photos untouched, creating new photos with the expected DPI and prefixed by the print size.

#!/usr/bin/perl # Set the short side, in inches $print_width = 4; $print_size = "4x6"; # # Do not edit below this point # # Create an array with every JPG in the current directory @list = `ls -1 IMG*jpg`; # Find out how many items are in the array $list_size = @list; # Loop through and set the DPI for 4x6 for ($x = 0; $x < $list_size; $x++) { # Store each image into a variable, one at a time $jpg = $list[$x]; chomp($jpg); # Get information on the file @info = split(" ", `identify -format "%w %h" $jpg`); $width = $info[0]; $height = $info[1]; chomp($height); # Determine the proper DPI by dividing the small side # by the number of inches we want it to be if ($width < $height) { $d = $width / $print_width; } else { $d = $height / $print_width; } # In case of lots of decimals, round to 3 $dpi = sprintf("%.3f", $d); # Print out the new information print $jpg . " : " . $dpi . "\n"; # Make and execute the command to create the new image $command = "convert " . $jpg . " -density " . $dpi . " " . $print_size . "-" . $jpg; system($command); }

This script assumes the photos come from a Canon camera, thus looking for photos beginning with "IMG". Edit as necessary.

Here is an example of it's use:

# ./ IMG_8258.jpg : 908.000 IMG_8262.jpg : 938.250 IMG_8263.jpg : 938.250 # ls -1 *jpg 4x6-IMG_8258.jpg 4x6-IMG_8262.jpg 4x6-IMG_8263.jpg IMG_8258.jpg IMG_8262.jpg IMG_8263.jpg



2010-03-28 08:54:47

mark says...
To use ImageMagick within Perl try PerlMagick. Here's a tutorial..

2010-04-02 19:57:05

slonkak says...
That's pretty cool. Though this tutorial was mostly for people to just copy/paste/run without downloading any additional libraries, PerlMagick looks pretty neat. Thanks.

Post a comment!