Resizing Images with ImageMagick

Sure, you can open up a graphics program like GIMP and resize an image, but what if you want to resize 10, 50 or 200 images? ImageMagick's convert program is just what you need.

I a previous article, I started a series on working with ImageMagick on the command line, but then I had to stop and deal with the massive migration project of moving my AskDaveTaylor.com site from one server to another while simultaneously dropping it into a completely different back-end software system—madness. I'm still fixing things and cleaning up the insane sprawl of it all.

So, my last article detoured into a discussion of scripts that helped with the migration process. I'm still working on these fast, short scripts, including one I wrote this morning:


for entry in blog/*
do
  new=$(echo $entry | sed 's/blog\///')
  echo "Redirect 301 $entry $new"
done

Can you track what this loop does? The only tricky part is the new=statement that removes blog/ from the filename matched in the for statement; otherwise, it's quite straightforward.

Seriously though, let's return to ImageMagick. There are a ton of things you can do with the command-line utilities. But first, let me look at where I left off.

I'd just shown a simple example of ImageMagick command-line tools to identify the dimensions of an image and use that as the basis of coming up with a scaled HTML img tag. Here's the script:


#!/bin/sh
identify=/usr/bin/identify
scale=$1
image=$2   # needs input validation code

height=$($identify $image | cut -d\   -f3 | cut -dx -f1)
width=$($identify $image | cut -d\   -f3 | cut -dx -f2)
newwidth="$(echo $width \* $scale | bc | cut -d. -f1)"
newheight="$(echo $height \* $scale | bc | cut -d. -f1)"
echo "<img src=$image height=$newheight width=$newwidth>"
exit 0

(Actually, I couldn't resist tweaking it slightly if you are keeping track, but I'm still being lazy and not validating the input as of yet. You can add that code easily enough.)

In use:


$ scaledown.sh 0.5 pvp.jpg
<img src=pvp.jpg height=152 width=485>

Okay, that's one way to make the display of the image be reduced on a Web page, but anyone who has done any work trying to speed up a Web site knows the huge problem here: reducing the container that displays an image doesn't reduce the image. The Web site visitor still has to download the original image, which is a huge waste of bandwidth and a performance hit.

So let's update the script to create a new, smaller version of the image as part of its output.

Enter the convert Command

The identify command is a great way to learn specific information about a graphical image file, but to manipulate it, you need to switch to convert.

There are a million command-line options to convert, but the one I use here is -resize, like this:


$ convert pvp-big.jpg -resize 0.5 pvp-0.5.jpg
$ identify pvp-big.jpg pvp-0.5.jpg
pvp-big.jpg JPEG 970x305 970x305+0+0 8-bit DirectClass 127kb
pvp-0.5.jpg JPEG 1x1 1x1+0+0 8-bit DirectClass 1.1kb

Hmmm...you can see what's happened, right? The image went from 970x305 to 1x1. Yikes.

How did that happen? The problem is that I'm handing the wrong kind of parameter to the -resize option. In fact, it wants a percentage (weirdly enough), so -resize 50% or -resize 50 both work:


$ convert pvp-big.jpg -resize 50 pvp-50.jpg
$ convert pvp-big.jpg -resize 50% pvp-50%.jpg
$ identify pvp*
pvp-50.jpg[1] JPEG 50x16 50x16+0+0 8-bit DirectClass 2.01kb
pvp-50%.jpg[2] JPEG 485x153 485x153+0+0 8-bit DirectClass 44.7kb
pvp-big.jpg[3] JPEG 970x305 970x305+0+0 8-bit DirectClass 127kb

A bit of mathematics reveals that -resize 50 meant that the width was scaled to 50 pixels, with the height proportionally scaled down to a tiny 16 pixels. -resize 50%, on the other hand, accomplished the goal, scaling the image down to a reasonable 485x153.

So the script will need users to enter a proper percentage amount or otherwise compensate. To make it more interesting, let's make the output filename gain a suffix that denotes the new geometry (as ImageMagick likes to refer to the height x width values). In this instance, the goal is to have pvp-big.jpg shrink 50% and be copied as pvp-big.285x153.jpg.

______________________

Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at www.DaveTaylorOnline.com.

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState