What is Python Wand?

Python Wand is a library that binds the functionalities of ImageMagick to Python. ImageMagick is a software suite that provides a command line interface to perform image processing tasks. Moreover, it can also be used to perform animations and image rendering.

Features

Using Python Wand, we can easily integrate ImageMagick in the Python programs. It enables us to perform operations like image reading, drawing, resizing, blurring, inverting, and cloning.

Installation

To install Python Wand, we have to run the following command.

pip install Wand
pip command to install Python Wand

Once we have installed Python Wand, we will install ImageMagick, as the library will use its functionalities. To install ImageMagick, we run the following commands in the terminal.

For Ubuntu or Debian-based Linux distributions:

sudo apt-get install libmagickwand-dev

For MacOS using brew installer:

brew install imagemagick

For MacOS using MacPorts:

sudo port install imagemagick

For Windows:

1. Visit the official website: https://imagemagick.org/script/download.php
2. Download the installer and run it
3. Follow the installation steps provided by the Wizard

Once we have installed Python Wand successfully, we can use it to perform operations on the images. In the next sections, we will be looking into the implementation of:

  • Reading an image

  • Blurring an image

  • Transform an image

  • Draw an image

Reading an image

To read an image, Python Wand provides us with the Image class that we import from wand.image. It is the primary class that we need to read and manipulate images. Please click the "Run" button below and view the image read by Wand.

import tkinter as tk
from PIL import Image, ImageTk

def display_image(image_path):
    # Create the main window
    root = tk.Tk()
    root.title("Image Display")

    # Load the image using PIL (Python Imaging Library)
    image = Image.open(image_path)

    # Get the dimensions of the image and the window
    img_width, img_height = image.size
    win_width, win_height = 2000, 1500  # Change these values as needed

    # Calculate the aspect ratio
    aspect_ratio = img_width / img_height

    # Determine the new size of the image to fit within the window
    if (img_width > win_width) or (img_height > win_height):
        if img_width > img_height:
            new_width = win_width
            new_height = int(new_width / aspect_ratio)
        else:
            new_height = win_height
            new_width = int(new_height * aspect_ratio)
        image = image.resize((new_width, new_height), Image.LANCZOS)  # Use Image.LANCZOS for versions 8.1.0 and above

    # Convert the image to a Tkinter PhotoImage object
    photo = ImageTk.PhotoImage(image)

    # Create a Label to display the image
    label = tk.Label(root, image=photo)
    label.pack()

    # Start the Tkinter event loop
    root.mainloop()

# Replace 'path_to_your_image.jpg' with the actual path to your image
image_path = 'image_1.jpg'
display_image(image_path)
Code to read an image using Python Wand

Code explanation

  • Lines 2–3: We import the Image class and display function from wand.image and wand.display respectively.

  • Line 6: We open image_1.jpg as a Wand image using the Image class and store it in the wand_pic variable.

  • Line 9: We print the image size on the "Terminal" tab using the size function provided by the Image class.

  • Line 10: We use the display function to show the image.

Bluring an image

To blur an image, we use the blur function provided by the Image class. To execute the code, please click the "Run" button below and view the blurred image in the "Output" tab.

import tkinter as tk
from PIL import Image, ImageTk

def display_image(image_path):
    # Create the main window
    root = tk.Tk()
    root.title("Image Display")

    # Load the image using PIL (Python Imaging Library)
    image = Image.open(image_path)

    # Get the dimensions of the image and the window
    img_width, img_height = image.size
    win_width, win_height = 2000, 1500  # Change these values as needed

    # Calculate the aspect ratio
    aspect_ratio = img_width / img_height

    # Determine the new size of the image to fit within the window
    if (img_width > win_width) or (img_height > win_height):
        if img_width > img_height:
            new_width = win_width
            new_height = int(new_width / aspect_ratio)
        else:
            new_height = win_height
            new_width = int(new_height * aspect_ratio)
        image = image.resize((new_width, new_height), Image.LANCZOS)  # Use Image.LANCZOS for versions 8.1.0 and above

    # Convert the image to a Tkinter PhotoImage object
    photo = ImageTk.PhotoImage(image)

    # Create a Label to display the image
    label = tk.Label(root, image=photo)
    label.pack()

    # Start the Tkinter event loop
    root.mainloop()

# Replace 'path_to_your_image.jpg' with the actual path to your image
image_path = 'blur1.png'
display_image(image_path)
Code to blur an image using Python Wand

The code explanation to blur an image using Wand is given below:

  • Line 5: We use the blur function which takes radiusDetermines the area size on which the blur effect is to be applied. and sigmaDetermines the intensity of the blur effect. as parameters.

  • Line 8: We use the save function to save the image. It only takes the filename to save the image.

Transform an image

Transforming an image is to apply operations on the image to alter the size, orientation, or color of the image. It is mostly used in computer vision algorithms before using images for a specific task. We can see the code to flip an image using Wand. Please click the "Run" button below:

import tkinter as tk
from PIL import Image, ImageTk

def display_image(image_path):
    # Create the main window
    root = tk.Tk()
    root.title("Image Display")

    # Load the image using PIL (Python Imaging Library)
    image = Image.open(image_path)

    # Get the dimensions of the image and the window
    img_width, img_height = image.size
    win_width, win_height = 2000, 1500  # Change these values as needed

    # Calculate the aspect ratio
    aspect_ratio = img_width / img_height

    # Determine the new size of the image to fit within the window
    if (img_width > win_width) or (img_height > win_height):
        if img_width > img_height:
            new_width = win_width
            new_height = int(new_width / aspect_ratio)
        else:
            new_height = win_height
            new_width = int(new_height * aspect_ratio)
        image = image.resize((new_width, new_height), Image.LANCZOS)  # Use Image.LANCZOS for versions 8.1.0 and above

    # Convert the image to a Tkinter PhotoImage object
    photo = ImageTk.PhotoImage(image)

    # Create a Label to display the image
    label = tk.Label(root, image=photo)
    label.pack()

    # Start the Tkinter event loop
    root.mainloop()

# Replace 'path_to_your_image.jpg' with the actual path to your image
image_path = 'flipped.png'
display_image(image_path)
Code to transform an image

Code explanation

  • Line 6: We use the clone function to create a copy of the image and store it in the flip variable.

  • Line 7: We use the flip function that flips the image by 180 degrees.

Drawing an image

Python Wand provides us with the necessary classes to draw images. In the code below, we will see how to draw a circle and save it as an image. Please click the "Run" button below to execute the code and view the circle created in the "Output" tab.

import tkinter as tk
from PIL import Image, ImageTk

def display_image(image_path):
    # Create the main window
    root = tk.Tk()
    root.title("Image Display")

    # Load the image using PIL (Python Imaging Library)
    image = Image.open(image_path)

    # Get the dimensions of the image and the window
    img_width, img_height = image.size
    win_width, win_height = 2000, 1500  # Change these values as needed

    # Calculate the aspect ratio
    aspect_ratio = img_width / img_height

    # Determine the new size of the image to fit within the window
    if (img_width > win_width) or (img_height > win_height):
        if img_width > img_height:
            new_width = win_width
            new_height = int(new_width / aspect_ratio)
        else:
            new_height = win_height
            new_width = int(new_height * aspect_ratio)
        image = image.resize((new_width, new_height), Image.LANCZOS)  # Use Image.LANCZOS for versions 8.1.0 and above

    # Convert the image to a Tkinter PhotoImage object
    photo = ImageTk.PhotoImage(image)

    # Create a Label to display the image
    label = tk.Label(root, image=photo)
    label.pack()

    # Start the Tkinter event loop
    root.mainloop()

# Replace 'path_to_your_image.jpg' with the actual path to your image
image_path = 'circle1.png'
display_image(image_path)
Code to draw an image

Code explanation

  • Lines 2–3: We import the Drawing class and the Color class from wand.drawing and wand.color respectively. The Drawing class provides us with the functions to create images, while the Color class allows us to add color to the images.

  • Line 5: We define the Drawing context that is used to create images.

  • Line 6: We use the stroke_color to set the outline color of our drawing. In our case, we have set it to white color using the Color class.

  • Line 7: We set the outline width using the stroke_width function.

  • Line 8: We fill our drawing with the color orange using the fill_color function.

  • Line 9: We draw the circle using the circle function. The circle function takes two parameters. The first one defines the coordinates where the center of the circle will lie, and the second one is the perimeter point which defines the radius/size of the circle.

  • Line 12: The draw function applies the defined circle to the Wand image.

Conclusion

Python Wand is a powerful library that enables us to manipulate images which makes it useful in computer vision and image processing. It helps integrate the ImageMagick suite into the Python ecosystem, making it a valuable tool for developers.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved