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.
In the October 2013 issue, 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.
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.
Rather than use the bc statements from the original script, let's make ImageMagick do the work by having this workflow:
Convert image to resized image and save as temp file.
Use identify to get new dimensions of temp file.
Create new filename based on geometry.
Rename temp file to new filename with geometry specified.
It turns out it's a lot less work, because mathematics are no longer required, which is a good thing!
The hardest part is to create the new filename, which involves more lines of code than the conversion itself. It involves figuring out the filename suffix, chopping the filename up and building a new one that inserts the new image geometry in the middle.
Here's the result (it's long):
#!/bin/sh convert=/usr/bin/convert identify=/usr/bin/identify resize=$1 source=$2 if [ -z "$resize" -o -z "$source" ] ; then echo "Usage: $0 resize sourcefile"; ;exit 1 fi if [ ! -r $source ] ; then echo "Error: can't read source file $source" ; exit 1 fi # let's grab the filename suffix filetype=$(echo $source | rev | cut -d. -f1 | rev) tempfile="resize.$filetype" # temp file name # create the newly sized temp version of the image $convert $source -resize $resize $tempfile # figure out geometry, the assemble new filename geometry=$($identify $tempfile | cut -d\ -f3 ) newfilebase=$(echo $source | sed "s/$filetype//") newfilename=$newfilebase$geometry.$filetype # rename temp file and we're done mv $tempfile $newfilename echo \*\* resized $source to new size $resize. result = $newfilename exit 0
That's it. It's not incredibly complicated if you go through it step by step. In fact, go back to the four-step algorithm I presented earlier. That's almost exactly duplicated in the comments within the script.
The only nuance is the sequence for newfilename assembly, which just strings together a series of variables to have their values tucked together.
Let's give it a whirl and see what happens:
sh resize.sh 50% pvp.jpg ** resized pvp.jpg to new size 50%. result = pvp.485x153.jpg
I'm skeptical, so let's test the new image file by using identify to get its dimensions:
$ identify pvp.485x153.jpg pvp.485x153.jpg JPEG 485x153 485x153+0+0 8-bit DirectClass 44.7kb
Perfect. More important, look at how the image size has shrunk as a result of it being scaled down 50%:
$ ls -l pvp.jpg pvp.485x153.jpg -rw-rw-r-- 1 taylor taylor 45751 Oct 9 04:14 pvp.485x153.jpg -rw-r--r-- 1 taylor taylor 130347 Sep 5 08:20 pvp.jpg
A definite win and a pretty handy script to keep around.
Of course, better positional parameter checking and a quick check to ensure that the resize parameter isn't something crazy would be good coding, but it's not a bad script that serves a very useful purpose.
So that's it. In my next article, I plan to take a look at adding embossing—text that's superimposed over a graphic—as an easy way to watermark sets of photos from the command line. Until then, cheerio!