Work the Shell - Messing around with ImageMagick
I've written previously about working with graphic images within shell scripts, and obviously, it's a little bit tricky because, well, scripts generally are strongest working with text, and you can't even see graphics, let alone manipulate them directly. Further, let's be candid, the suite of utilities included with a stock Linux/UNIX system doesn't include much that help you work with graphics or image files at all.
Fortunately, there's a splendid open-source package called ImageMagick, which actually is designed to make working with image files from the command line easy and fast. It's the smart back end to a bunch of image utilities, and with a quick trip to www.imagemagick.org, you can download it too.
A couple different steps are involved in installing it, and this time, I'm actually going to play with my Apple MacBook Pro and install the utilities to live within the Darwin world of Mac OS X.
Since 99% of the time that I'm using my Mac I am logged in as taylor, I'm going to opt to drop the software into my own personal bin directory rather than the more standard location of /usr/local/src (with the binary in /usr/local/bin). It might be that I'm a long-term UNIX geek or something, but I have my own ~/bin (or $HOME/bin, if you prefer) directory anyway, so once the binary file was downloaded, here's what I did:
cd ../bin tar xvf ../Downloads/ImageMagick-i386-apple-darwin9.6.0.tar
Because this particular distro includes precompiled binaries, it's as easy as just tweaking a few environment variables to add the unpack directory and proceed:
export MAGICK_HOME="/Users/taylor/bin/ImageMagick-6.5.2" export PATH="$MAGICK_HOME/bin:$PATH" export DYLD_LIBRARY_PATH="$MAGICK_HOME/lib"
These are best added to your ~/.profile or ~/.cshrc (if you're using Csh, but why would you?), so that they're invoked each and every time you log in or, in the case of the Mac environment, spawn a new Terminal shell.
It's a good idea to test the newly installed programs too. Find a .gif, .jpg or .png file and see what the ImageMagick identify program says. Here's how I did that:
$ find . -name "*png" -o -name "*.jpg" -o -name "*gif" ./iphone-id.png $ identify iphone-id.png iphone-id.png PNG 470x118 470x118+0+0 8-bit DirectClass 12.2kb
It's more useful than the file command, which reports:
$ file iphone-id.png iphone-id.png: PNG image data, 470 x 118, 8-bit/color RGB, non-interlaced
Where identify really shines is with JPEG files, which the file command can't quite seem to figure out. Why that's true, I don't know, but that shortcoming is the main reason I have ImageMagick installed on my system.
One of my hobbies is photography, and as a parent, I find that I frequently end up as the “official” photographer for school events. I recently did just that for my daughter's May Fair event, and I ended up with about 500 5–8MB image files that were great for printing (about 4,200x2,800) but not so good for viewing on the computer screen. What I wanted to do was create images that were approximately 1,024x800 or thereabouts, so that they'd view at 100% on a typical computer screen, in a directory that paralleled the original image file directory. That way, parents could view a slideshow of the smaller images and then grab the identically named big image if they wanted to upload it and order prints.
With ImageMagick, this is easy. In fact, if I wanted to use the mogrify command, I could have very easily done everything in a single command, but because I like obscure, complicated solutions rather than simple, elegant ones, I decided to use the convert command instead.
The challenge is that, like everything else in ImageMagick, the convert app has a staggering number of different command flags. Type convert, and you'll see what I mean.
Digging through them, here's the flag I want to use:
-resize geometry resize the image
That sounds like what we need is to resize the images, though “geometry” is still a bit of an unknown. Now it's time to pop over to the ImageMagick Web site, where we find a ton of options for geometry, including:
scale%: height and width both scaled by specified percentage.
scale-x%xscale-y%: height and width individually scaled by specified percentages.
width: width given, height automatically selected to preserve aspect ratio.
xheight: height given, width automatically selected to preserve aspect ratio.
widthxheight: maximum values of height and width given, aspect ratio preserved.
To accomplish the conversion we want, we simply can specify the desired width and let the utility do all the work:
$ identify DSC_7466.JPG DSC_7466.JPG JPEG 4288x2848 4288x2848+0+0 ↪8-bit DirectClass 8.148mb $ convert -resize 1024 DSC_7466.JPG smaller-DSC_7466.JPG $ identify smaller-DSC_7466.JPG smaller-DSC_7466.JPG JPEG 1024x680 1024x680+0+0 8-bit ↪DirectClass 776kb
As hoped, the 4,288x2,848 image is shrunk down to 1,024x680, and the new, smaller image is saved with the new filename.
Great! A quick mkdir smaller, and we're in business, so I utilize a shell for loop to iterate through the 500 images:
for filename in *.png do convert -resize "50%" $filename smaller/$filename done
Once you've gone through the hassle of installing the ImageMagick program, it's delightful to see how easily many different tasks can be accomplished.
Dave Taylor has been involved with UNIX since he first logged in to the on-line network in 1980. That means that, yes, he's coming up to the 30-year mark now. You can find him just about everywhere on-line, but start here: www.DaveTaylorOnline.com. In addition to all his other projects, Dave is now a film critic. You can read his reviews at www.DaveOnFilm.com.
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.
- October 2014 Issue of Linux Journal: Embedded
- Encrypt Your Dog (Mutt and GPG)
- Practical Tiny Core in the Fire Service
- Tech Tip: Really Simple HTTP Server with Python
- DevOps for Dummies
- Python Scripts as a Replacement for Bash Utility Scripts
- New Products
- New Products
- Open Axiom
- Returning Values from Bash Functions
Free DevOps eBooks, Videos, and more!
Regardless of where you are in your DevOps process, Linux Journal can help!
We offer here the DEFINITIVE DevOps for Dummies, a mobile Application Development Primer, and advice & help from the expert sources like:
- Linux Journal