A Master Class on ImageMagik

Master the swiss army knife of image processing

ImageMagick is a powerful command-line utility and library for reading, writing, and manipulating images in various formats. It can read, convert and write images in over 200 formats including PNG, JPEG, GIF, HEIC, TIFF, DPX, EXR, WebP, and PDF. ImageMagick has been around since 1987, making it one of the longest-running open-source image processing tools available. It was originally developed by John Cristy at DuPont. The software is widely used in web applications, automated image processing workflows, and various software that needs image manipulation capabilities.

Installation is pretty easy and supported in nearly every platform

# Install on windows
choco install imagemagick
# Or download the installer from the official website
# https://imagemagick.org/script/download.php#windows

# Debian/Ubuntu
sudo apt-get update
sudo apt-get install imagemagick

# CentOS/RHEL
sudo yum install imagemagick

# Fedora
sudo dnf install imagemagick

# Arch Linux
sudo pacman -S imagemagick

# On Mac Using Homebrew
brew install imagemagick

# On Mac Using MacPorts
sudo port install imagemagick

While ImageMagick can do a lot more it is most commonly used to convert image formats from one type to another type. Here are a few of the most common image conversions:

#jpg to png
convert image.jpg -quality 100 image.png

#jpg to webp (webp is a web image format)
convert image.jpg -quality 100 image.webp

#png to webp
convert image.png -quality 100 image.webp

#heic to png. NOTE: HEIC (High Efficiency Image Container) is a file format that uses the HEIF (High Efficiency Image Format) standard. It was adopted by Apple for iOS devices starting with iOS 11 in 2017.
convert image.heic -quality 100 image.png

You can also resize images. There are two main resize arguments, thumbnail and resize.

The -thumbnail operation:

  • Strips all metadata including EXIF

  • Uses a faster resampling algorithm (point sampling)

  • Applies simplified filtering

  • Reduces color precision during processing

  • Can process images up to 3x faster than -resize

The -resize operation:

  • Preserves all metadata

  • Uses higher quality resampling algorithms

  • Maintains full color fidelity

  • Computationally more expensive

# Resize to specific dimensions (maintaining aspect ratio)
convert input.jpg -resize 800x600 output.jpg
#or
convert input.jpg -thumbnail 800x600 output.jpg

# Resize to specific width, height auto-calculated
convert input.jpg -resize 800 output.jpg

# Force specific dimensions (ignoring aspect ratio)
convert input.jpg -resize 800x600! output.jpg

# Resize to percentage of original
convert input.jpg -resize 50% output.jpg

When an image is taken from a digital camera or smart phone the orientation of the image, along with other things, is stored inside the image in a container called EXIF (Exchangeable Image File Format). ImageMagick can use this EXIF data to rotate an image correctly.

# Auto-rotate based on EXIF data
convert input.jpg -auto-orient -quality 100 output.jpg

You may have a bunch of images in a folder and need to resize them all. For this imageMagick has a command called mogrify.

mogrify -resize 1080x1080 -quality 100 *.jpg

There is a plethora of advanced features in ImageMagick making it a very useful for the novice and advanced alike. Because of its power, nearly all scripting languages have an ImageMagick interface (python has three: PythonMagick, Wand, or pgmagick).

ImageMagick comes with several command-line utilities beyond just convert and mogrify. The main commands include:

  1. identify - Display image characteristics (format, dimensions, color depth, etc.)

  2. composite - Overlay images on top of one another

  3. montage - Create composite image by combining several separate images

  4. compare - Mathematically and visually compare images

  5. conjure - Interpret and execute scripts in the Magick Scripting Language (MSL)

  6. stream - Stream portions of an image or apply operations to an image incrementally

  7. import - Capture screenshot or image from an X server

  8. display - View images on an X server (interactive viewer)

  9. animate - Display animated image sequences on an X server

  10. magick - The unified command-line interface (newer versions)

In newer versions of ImageMagick, magick is the recommended primary command, with operations specified as subcommands.

# Convert a JPEG to PNG with resizing
magick convert input.jpg -resize 800x600 output.png

# Create a thumbnail with border
magick convert input.jpg -thumbnail 120x120 -border 2 -bordercolor black thumbnail.jpg

# Apply multiple effects in sequence
magick convert input.jpg -sepia-tone 80% -vignette 0x15 -rotate 3 vintage_effect.jpg

# Batch convert multiple files
magick mogrify -format png -quality 90 -resize 1024x768 *.jpg

# Composite images
magick composite overlay.png background.jpg -gravity center result.jpg

# Create animated GIF from sequence
magick convert frame*.png -delay 20 -loop 0 animation.gif

You may have noticed the conjure command above. ImageMagick also has a scripting language called MSL. The Magick Scripting Language (MSL) is an XML-based scripting language specific to ImageMagick that allows you to create complex image processing routines in a script format.

<?xml version="1.0" encoding="UTF-8"?>
<imagemagick>
  <read filename="input.jpg"/>
  <resize geometry="50%"/>
  <blur radius="0" sigma="3"/>
  <border width="5" height="5" fill="black"/>
  <write filename="output.jpg"/>
</imagemagick>

#if the above XML was saved as script.msl you can call it this way:
magick conjure script.msl

For anyone building image-centric applications, ImageMagick offers a robust foundation that scales from single-user desktop applications to high-throughput server environments processing millions of images daily. I use it all the time and highly recommend it ;)

Thanks for reading. Please like and share! Let me know of other topics that interests you.