Automating Image Resizing

In this day and age this should be trivial an easy. But to do it right is still not widely known. Applications like Adobe Lightroom can optionally do output sharpening (which I think is a desirable feature). But since I’ve abandoned Adobe products, I’m left to my own devices. So my solution to this was a Makefile and ImageMagick. My script is based on a tutorial called Sharpening using ImageMagick.Here is the Makefile:

# Make thumbnail, small, medium and large images sharpened and compressed
# correctly for wordpress.
#
# Instructions taken from:
# http://redskiesatnight.com/2005/04/06/sharpening-using-image-magick/

CONVERT=/sw/bin/convert
THUMB_SIZE=160x160
SMALL_SIZE=320x320
MEDIUM_SIZE=640x640
LARGE_SIZE=1280x1280
QUALITY=80

# unsharp mask settings...
# radius = output ppi / 150
RADIUS=0.667
# if radius < 1, then sigma = radius
# else sigma = sqrt(radius)
SIGMA=0.667
# -unsharp <radius>{x<sigma>}{+<amount>}{+<threshold>}
UNSHARP=$(RADIUS)x$(SIGMA)+0.5+0.07

SOURCES=aftermath.jpg bumble-bee.jpg cochise.jpg gladstone-cow.jpg nuclear-flowers.jpg walk.jpg whitianga-beach.jpg
THUMB_FILES=$(SOURCES:.jpg=-t.jpg)
SMALL_FILES=$(SOURCES:.jpg=-s.jpg)
MEDIUM_FILES=$(SOURCES:.jpg=-m.jpg)
LARGE_FILES=$(SOURCES:.jpg=-l.jpg)
OUTPUTS=$(THUMB_FILES) $(SMALL_FILES) $(MEDIUM_FILES) $(LARGE_FILES)

help:
 @echo "Create resized images for:"
 @echo " $(SOURCES)"
 @echo
 @echo "Use 'make all' to generate all images"

all: $(OUTPUTS)
thumb: $(THUMB_FILES)
small: $(SMALL_FILES)
medium: $(MEDIUM_FILES)
large: $(LARGE_FILES)

clean:
 rm -f $(THUMB_FILES)
 rm -f $(SMALL_FILES)
 rm -f $(MEDIUM_FILES)
 rm -f $(LARGE_FILES)

%-t.jpg: %.jpg
 $(CONVERT) -resize $(THUMB_SIZE) $< -unsharp $(UNSHARP) -quality $(QUALITY) $@

%-s.jpg: %.jpg
 $(CONVERT) -resize $(SMALL_SIZE) $< -unsharp $(UNSHARP) -quality $(QUALITY) $@

%-m.jpg: %.jpg
 $(CONVERT) -resize $(MEDIUM_SIZE) $< -unsharp $(UNSHARP) -quality $(QUALITY) $@

%-l.jpg: %.jpg
 $(CONVERT) -resize $(LARGE_SIZE) $< -unsharp $(UNSHARP) -quality $(QUALITY) $@

Leave a Reply

%d bloggers like this: