provides software and services that enable enterprises
Live Chat 1-888-673-6564

Open Source Software Technical Articles

  • Home
  • Search
  • Contact Us
  • Products and Support
  • Services
  • Enterprise OSS Blog
  • Wazi Technical Blog
  • About Wazi
  • Attributions and Licensing
  • Supply Chain Compliance
  • How to Contribute
  • Contributors
  • Resources Library
  • Cloud Services
  • Partners
  • Customers
  • Community
  • Company
  • Careers
  • News and Events

Subscribe to Wazi by Email

Your email:


Enterprise Developer Support 24 x 7, Get a Support Quote Now!


click-here-to-chat-with-an-online-representative

download-oss-discovery

Latest Posts

  • The secret to great reporting with Drupal 7
  • A more colorful LibreOffice unveiled
  • Toward a more colorful LibreOffice
  • Flexible administration with Puppet's Facter and templates
  • Knock for OpenSSH
  • Get more out of phpMyAdmin
  • Image annotation in GIMP, Dia, and OpenOffice Draw
  • Solr, Drupal 7, and faceted search
  • Using FreeNAS' new full disk encryption for ZFS
  • Create distributed storage with Gluster

Connect with Us!

Current Articles | RSS Feed RSS Feed

Become an ImageMagick Ninja: Doing Things in Batches

Posted by Carla Schroder on Tue, Sep 20, 2011
  
Email This Email Article  
Tweet  
  

Last month we introduced ImageMagick, a software suite that lets you manipulate images in several interesting ways. In that article we learned a lot of ways to resize images, make thumbnails, and convert image file formats. Today we're going to unleash more of the mighty ImageMagick power and learn to make drop shadows, raised buttons, and proof sheets, and generate different sizes of the same image.



As always, before you manipulate irreplaceable photos, back up your originals first!



Before we start talking about more ImageMagick functionality, here's a tip that can help you with all ImageMagick commands. Using ImageMagick commands in single-line bash shell scripts lets you quickly modify multiple images in one batch. Even if you prefer using a different programming or scripting language, bash one-liners are useful for testing whether your ImageMagick commands are doing what you want. Consider this command:



$ for file in *.jpg; do convert $file -resize 640x480 640x480-$file; done


This resizes all the JPG images in the current directory to 640x480, preserves the originals, and creates new files with 640x480- prepended to the filenames. It is easy to customize this for different file types, different convert command options, and different conventions for naming the new files. Tweak it just a little and you can create thumbnails:




$ for file in *.jpg; do convert $file -thumbnail 125x -quality 85 tn-$file; done


This creates 125-pixel-wide thumbnails out of all the JPGs in the current directory, at 85 JPG quality level, and prepends the filenames with tn. As before, the originals are preserved.



By the way, even though it's not an ImageMagick command, here's a way to handle the common annoyance of mixed upper- and lower-case file extensions. Windows applications and a lot of digital cameras like to shout at you by putting file extensions in all caps. Windows is not case-sensitive, but Linux and Unix are. Mac OS X is extra fun because the HFS filesystem is not case-sensitive, but HFS+ is. One way to handle this is to list both cases in your bash invocation; for example, for file in * .jpg *.JPG;. But this doesn't really solve the problem. Why not change all the extensions to lowercase instead:



$ for file in *.JPG; do mv $file ${file%%.JPG}.jpg; done


Drop Shadows

You can apply bash one-liners to any ImageMagick command, so let's learn a few more. Suppose you want to add a drop shadow border to an image. Adding the shadow option to the convert command is not enough by itself; you need to string several options together correctly. The incantation below works for any image format that supports transparency, such as PNG, BMP, and TIFF:




$ convert image1.png \( +clone -background black -shadow 60x5+30+40 \) +swap -background none -layers merge +repage image2.png


This command adds a drop shadow on the right and bottom of image1.png and renames it image2.png, so the original is unchanged. It is not solid black but 60% opacity; higher values are more opaque. The 5 value creates a soft rounded edge; 0 is a hard edge with square corners, and the higher the value, the softer the edge. +30 is the x or horizontal axis value in pixels. + places the shadow on the right and - places it on the left. +40 is the y, or vertical axis value. + places the shadow at the bottom and - places it at the top. You don't have to have shadows on two sides; you can use zero, like +0+30, to create a shadow on a single border.



Image formats that do not support transparency, like GIF and JPG, must swap in a white background:



$ convert deer1.jpg \( +clone -background black -shadow 60x5+30+40 \) +swap -background white -layers merge +repage deer2.png


Figure 1 shows the result.



[caption id="attachment_87607" align="alignnone" width="590" caption="Fig. 1: The little buck that ate my tomatoes and beans, with a stylish drop shadow."][/caption]

The +clone -background color can be any color. Use the color names or codes on the ImageMagick Color Names page.



What if you want to add nice drop shadows to a whole batch of images?



$ for file in *.jpg; do convert $file \( +clone -background black -shadow 60x5+30+40 \) +swap -background white -layers merge +repage shadow-$file; done


19a98812-f823-48dc-841e-bf029c63c6d7

Making Raised Buttons



Another thing you might want to do with images is turn your thumbnails into raised buttons:



$ convert thumbnail.jpg -raise 10 raised-thumbnail.jpg


This creates a 10-pixel-wide border with shadows on the right and bottom (Figure 2). If you use +raise then ImageMagick puts the shadows on the other sides, which is supposed to make it look sunken.



[caption id="attachment_87608" align="alignnone" width="200" caption="Fig. 2: Thumbnail of the garden-eating little buck as a raised button."][/caption]

How do you make a batch of these? I think you know by now!



Proof Sheet



Old geezers like me have happy memories from the olden days of image proof sheets, such as contact prints of photographic negatives, which are the ancestors to thumbnails on web pages. Proof sheets are still useful, because they let you print pages of reduced-size images for quick review. ImageMagick's montage command creates proof sheets with ease:



$ montage '*.jpg' -geometry 200x+15+5 proof-sheet.jpg


This command takes all the JPG files in the current directory, creates thumbnails 200 pixels wide, spaces them in rows 15 pixels apart and columns 5 pixels apart, and outputs a single JPG file. Your output file can be any image format you want.



Generating Different Sizes of the Same Image



In addition to smaller copies of multiple images, you might sometimes want to have different sizes of the same image. This example makes three different sizes of a flower picture to make wallpaper for different screen resolutions:



$ convert flower.png \
\( +clone -resize 1280x -write flower-1280.png +delete \) \
\( +clone -resize 1024x -write flower-1024.png +delete \) \
-resize 800x flower-800.png


For each resizing operation a new clone is made of the original, then deleted when the resized file is created, so each copy is made from the original, rather than from increasingly degraded copies of reduced-size copies.

Scripting and APIs



All of this bash goodness is useful and fun, but what if you prefer to use a different programming or scripting language? Visit the ImageMagick project page Convert, Edit, or Compose Images From Your Favorite Programming Language to learn about ImageMagick's ready-made interfaces for a large number of languages. There are special helpers for C, C++, Ruby, Perl, Python, PHP, and many more.



Fred's ImageMagick Scripts is a treasure trove of bash scripts for dozens of different effects, such as pagecurl emboss, fisheye2pano, stainedglass, rotate3d, and many more. These are useful even when you're not scripting in bash because Fred Weinhaus, the generous author of these scripts, has documented the ImageMagick commands and options in each script thoroughly, with an abundance of before and after images to illustrate. It's one of the best ImageMagick references anywhere.

Follow @openlogic
Follow @CloudSwing

This work is licensed under a Creative Commons Attribution 3.0 Unported License
Creative Commons License.Follow @openlogic
Follow @OSCloudServices

This work is licensed under a Creative Commons Attribution 3.0 Unported License
Creative Commons License.
Tags: Technical, Tutorial, Utility, ImageMagick

Comments

Currently, there are no comments. Be the first to post one!
Post Comment
Name
 *
Email
 *
Website (optional)
Comment
 *

Allowed tags: <a> link, <b> bold, <i> italics

Loading...
Error sending email
Email sent successfully

Email article
Email To : 
Your name : 
Message : (maximum 200 characters)
Home | Search | Contact Us | Products and Support | Services | Enterprise OSS Blog | Wazi Technical Blog | Resources Library | Cloud Services | Partners | Customers | Community | Company | Careers | News and Events
Products
OpenLogic Exchange (OLEX)
License Compliance Module
OSS Discovery
OSS Deep Discovery
OpenUpdate
Services
Open Source Support
CentOS Support
Scanning & Compliance
Open Source Training
Professional Services
Solutions
Support & Indemnification
Open Source Governance
Open Source Scanning
Open Source Provisioning
Consulting & Training
Contact Us
1-888-673-6564


© 2013 OpenLogic, Inc. All rights reserved.
Site Map  |  Privacy Policy