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.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
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
| 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 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- RSS Feeds
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- Developer Poll
- Dart: a New Web Programming Experience
- What's the tweeting protocol?
- May 2013 Issue of Linux Journal: Raspberry Pi
- Reply to comment | Linux Journal
1 hour 31 min ago - Reply to comment | Linux Journal
2 hours 17 min ago - Web Hosting IQ
3 hours 51 min ago - Thanks for taking the time to
5 hours 28 min ago - Linux is good
7 hours 26 min ago - Reply to comment | Linux Journal
7 hours 43 min ago - Web Hosting IQ
8 hours 13 min ago - Web Hosting IQ
8 hours 13 min ago - Web Hosting IQ
8 hours 14 min ago - Reply to comment | Linux Journal
11 hours 15 min ago
Enter to Win an Adafruit Prototyping Pi Plate 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 Prototyping Pi Plate 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
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
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.




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!