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.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- New Products
- Linux Systems Administrator
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Using Salt Stack and Vagrant for Drupal Development
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Reply to comment | Linux Journal
5 hours 43 min ago - Reply to comment | Linux Journal
5 hours 59 min ago - Favorite (and easily brute-forced) pw's
7 hours 50 min ago - Have you tried Boxen? It's a
13 hours 42 min ago - seo services in india
18 hours 14 min ago - For KDE install kio-mtp
18 hours 14 min ago - Evernote is much more...
20 hours 15 min ago - Reply to comment | Linux Journal
1 day 5 hours ago - Dynamic DNS
1 day 5 hours ago - Reply to comment | Linux Journal
1 day 6 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




Comments
Shame on you
"The article you are trying to access requires you to be a registered Linux Journal print or digital subscriber."
Shame on you for putting articles in an rss feed that require us to login or subscribe in order to read them. I'll remember to avoid your site completely from now on.
Nice way to lose viewers!