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.

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix