An Intro to Image Processing with Wand / ImageMagick and Python

ImageMagick is an open-source tool that you can use to create, edit, compose, or convert digital images. It supports over 200 image formats. According to its website, ImageMagick can resize, flip, mirror, rotate, distort, shear, and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses, and Bézier curves.

For more information about ImageMagick, you should go to their website.

Wand is a Python wrapper around ImageMagick. Wand has many similar features to Pillow and is the closest thing you could label as an alternative to Pillow. Wand is easy to install with pip:

python3 -m pip install wand

You have to have ImageMagick installed too. Depending on which operating system you are using, you may need to set up an environment variable as well. See the documentation for Wand if you have any issues getting it installed or configured.

Wand can do many different image processing tasks. In the next few sections, you will see how capable it is. You will start by learning about Wand’s image effects!

 

Applying Image Effects with Wand

Wand has several different image effects that are built-in. Here is a full listing:

  • Blur
  • Despeckle
  • Edge
  • Emboss
  • Kuwahara
  • Shade
  • Sharpen
  • Spread

Some of these effects are present in Pillow and some are not. For example, Pillow does not have Despeckle or Kuwahara.

To see how you can use these effects, you will use this ducklings photo:

ducklings

You will try out Wand’s edge() method. Create a new Python file and name it wand_edger.py. Then enter the following code:

# wand_edger.py

from wand.image import Image


def edge(input_image_path, output_path):
    with Image(filename=input_image_path) as img:
        img.transform_colorspace("gray")
        img.edge(radius=3)
        img.save(filename=output_path)

if __name__ == "__main__":
    edge("ducklings.jpg", "edged.jpg")

The first new item here is the import: from wand.image import Image. The Image class is your primary method of working with photos in Wand. Next, you create an edge() function and open up the duckling’s photo. Then you change the image to grayscale. Then you apply edge(), which takes in a radius argument. The radius is an aperture-like setting.

When you run this code, the output will look like this:

Duckings with Edge Effect

You should experiment with different values for radius. It changes the result.

Now let’s take a look at the special effects that Wand provides.

 

Special Effects

Wand supports quite a few other effects that they have dubbed “Special Effects”. Here is a list of what is currently supported:

  • Add Noise
  • Blue Shift
  • Charcoal
  • Color Matrix
  • Colorize
  • FX
  • Implode
  • Polaroid
  • Sepia Tone
  • Sketch
  • Solarize
  • Stereogram
  • Swirl
  • Tint
  • Vignette
  • Wave
  • Wavelet Denoise

Some of these are fun or interesting. The documentation has before-and-after photos for all these examples.

You will try using Vignette in this section using the photo of the author:

Michael Driscoll
Michael Driscoll

Create a new file named wand_vignette.py and add this code to it:

# wand_vignette.py

from wand.image import Image


def vignette(input_image_path, output_path):
    with Image(filename=input_image_path) as img:
        img.vignette(x=10, y=10)
        img.save(filename=output_path)


if __name__ == "__main__":
    vignette("author.jpg", "vignette.jpg")

In this example, you call vignette(). It takes several different arguments, but you only supply x and y. These arguments control the amount of edging that goes around the image that you are adding a vignette to.

When you run this code, you will get the following output:

Vignette effect

 

That looks nice. This is a fun way to make your photos look unique and classy. Give it a try with some of your photos!

Now you are ready to learn how to crop with Wand.

 

Cropping with Wand

Cropping with Wand is similar to how Pillow crops. You can pass in four coordinates (left, top, right, bottom) or (left, top, width, and height). You will use the duckling’s photo and find out how to crop the photo down to only the birds.

Create a new file and name it wand_crop.py. Then add the following code:

# wand_crop.py

from wand.image import Image


def crop(input_image_path, output_path, left, top, width, height):
    with Image(filename=input_image_path) as img:
        img.crop(left, top, width=width, height=height)
        img.save(filename=output_path)


if __name__ == "__main__":
    crop("ducklings.jpg", "cropped.jpg", 100, 800, 800, 800)

For this example, you supply left, top, width, and height. When you run this code, the photo will be cropped to look like this:

Cropped ducklings

You can experiment with different values to see how it affects the crop.

Wand can do much more than what is demonstrated in this section. It can do most of the same things as Pillow and more. Pillow’s main benefit over Wand is that Pillow is written in Python and doesn’t require an external binary like Wand does (i.e., ImageMagick).

Wrapping Up


The Wand package is quite powerful. It can do most of that same things that the Pillow package can do and a few things that it can’t. Of course, Pillow also has features that Wand does not. You should definitely check out both packages to see which one is a better fit for what you are doing.

Both packages are great ways to edit and manipulate images with Python. Give Wand a try and use some of its many other effects to see just how powerful it is!