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

  • 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
  • How to set up Solr 4.2 on Drupal 7 with Apache

Connect with Us!

Current Articles | RSS Feed RSS Feed

Become an ImageMagick Ninja: Resizing And Converting Images

Posted by Carla Schroder on Mon, Aug 22, 2011
  
Email This Email Article  
Tweet  
  

Want a powerful, scriptable bitmap image editor that creates, edits, and converts images? ImageMagick is a suite of 11 commands and a GUI that let you resize, flip, mirror, rotate, distort, and shear pixel-based images. With it, you can adjust colors, create special effects, and draw text, lines, polygons, ellipses, and Bezier curves. ImageMagick supports more than 100 image file formats, and offers APIs for all popular programming languages.



ImageMagick is a useful tool for webmasters who need to create web photo galleries, resize images and create thumbnails, and automatically resize user-uploaded images. In brief:




    • animate animates a sequence of images

    • compare displays the difference between an image and its reconstruction

    • composite overlaps images

    • conjure processes a Magick Scripting Language script

    • convert converts images to different image file formats and applies special effects

    • display displays an image in the ImageMagick viewer

    • identify shows image file information such as size, format, colors, bit depth, and more

    • import provides a screenshot tool

    • mogrify resizes, blurs, crops, despeckles, and performs other image editing operations

    • montage combines several images into a composite

    • stream extracts any image pixel component, such as colors, color space, or section of an image, and exports it to a file



Want to see just how powerful ImageMagick is? Let's start by trying some useful commands to resize images – but before you begin working on an image with ImageMagick, make sure you have it backed up.




The convert command syntax, as explained in man convert, is convert [input-options] input-file [output-options] output-file. Input and output options are, um, optional, but filenames are required. This simple example resizes a PNG file:




$ convert image.png -resize 150x100 image-s.png



ImageMagick GUI
ImageMagick also includes a simple GUI interface that you open with the display commmand. Run display with no options to bring up an ImageMagick screen with its wizard logo. Left-click on this screen to open the complete menu; right-click to open a Shortcut menu. Or you can open an image directly with display imagename (see below – click to enlarge).




If you give your input and output files the same names then your original image will be overwritten with the output of the command. If you give them different names then your original file will not be changed, and you will have two files. The resize value of 150x100 is called geometry. Geometry is a big deal in ImageMagick, so let's take a moment to make sure we understand it. Width and height values are always in pixels. When both width and height values are given, the width value always comes first. The geometry values must have no spaces, so 150 x 100 would not work. In our simple example 150x100 means "These are the maximum sizes, and preserve the aspect ratio." In other words, ImageMagick will fit your image into a 150x100 pixel rectangle by altering the width and height dimensions, without changing the aspect ratio. It may not result in an image that's exactly 150x100 pixels, but it will be as close as it can get.



If you want to specify the exact width and preserve the aspect ratio, then specify only the width:




$ convert image.png -resize 150 image-s.png


To set the exact height and preserve the aspect ratio, specify only the height value prefixed with x, like this:




$ convert image.png -resize x100 image-s.png


When you need your image to be an exact size, without preserving the aspect ratio, append an exclamation point to the geometry:




$ convert image.png -resize 100x100! image-s.png


You can tell ImageMagick to only resize images that are larger or smaller than your geometry by using the greater-than and less-than signs. The command below resizes only images that are larger than your geometry:




$ convert image.png -resize 64x64> image-s.png


And this resizes only images that are smaller:




$ convert image.png -resize 64x64< image-s.png


One more useful option is resizing by percentage. Values under 100% reduce the size of your image, and values over 100% increase it. This example doubles the image size:




$ convert image.png -resize 200% image-s.png


Depending on which command shell you are using, you may need to escape the exclamation point, greater than, less than, and percent signs.



Making Thumbnails



Creating thumbnails with ImageMagick is so easy you will fall over in happiness. This example creates 100-pixel-wide thumbnails of all the JPG files in the current directory. Your originals are preserved and the thumbnails retain the original filenames with sequential numbers appended – image-0.jpg, image-1.jpg, and so on:




$ convert -thumbnail 100 *.jpg


The thumbnail option is special because it does more than just resize the images; it is optimized for speed.


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

Convert Formats



The convert command can also convert file formats. The simplest way to use it is:




$ convert image.png image.jpg


You can fine-tune this any number of ways, such as by specifying size and .jpg compression level:




$ convert image.png -resize 240x320 -quality 85 image.jpg


JPG uses lossy compression, and possible values are 1 to 100, with higher values resulting in better-quality images with larger file sizes. The default is the same as your input image, if ImageMagick can determine what it is; otherwise it is 92.



PNG compression levels range from 0 to 9, from least to most. PNG compression is lossless, so higher levels of compression do not change image quality. While this sounds simple, there are many fine points to PNG compression that you can read about if you are interested in maximum control and squeezing files as small as possible.



In fact, to you can learn more about any aspect of ImageMagick from its documentation. Each command has its own man page, and the master man page is man imagemagick. Extended documentation online includes:




    • Convert, Edit, or Compose Images from the Command-line has detailed command and usage descriptions

    • Magick++ User-Level ClassesImage is useful to users as well as coders

    • Convert, Edit, and Compose Images is a good starting point, with links to detailed information

    • Convert, Edit, or Compose Images From Your Favorite Programming Language details the many programming and scripting language APIs



Even though the documentation is large and thorough, it could be better organized. Many ImageMagick howtos tend to gloss over essential details, which is understandable because ImageMagick has hundreds of command options, and finding important details often requires some detective work. But once you master the important fundamentals the whole works makes sense, and you are on your way to becoming an ImageMagick ninja.



Your next step should be a look at my next article on using ImageMagick to do things in batches.

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