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
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.php2. Download the installer and run it3. 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 explanation
Lines 2–3: We import the
Imageclass anddisplayfunction fromwand.imageandwand.displayrespectively.Line 6: We open
image_1.jpgas a Wand image using theImageclass and store it in thewand_picvariable.Line 9: We print the image size on the "Terminal" tab using the
sizefunction provided by theImageclass.Line 10: We use the
displayfunction 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)
The code explanation to blur an image using Wand is given below:
Line 5: We use the
blurfunction which takes andradiusDetermines the area size on which the blur effect is to be applied. as parameters.sigmaDetermines the intensity of the blur effect. Line 8: We use the
savefunction to save the image. It only takes thefilenameto 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 explanation
Line 6: We use the
clonefunction to create a copy of the image and store it in theflipvariable.Line 7: We use the
flipfunction 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 explanation
Lines 2–3: We import the
Drawingclass and theColorclass fromwand.drawingandwand.colorrespectively. TheDrawingclass provides us with the functions to create images, while theColorclass allows us to add color to the images.Line 5: We define the
Drawingcontext that is used to create images.Line 6: We use the
stroke_colorto set the outline color of our drawing. In our case, we have set it towhitecolor using theColorclass.Line 7: We set the outline width using the
stroke_widthfunction.Line 8: We fill our drawing with the color orange using the
fill_colorfunction.Line 9: We draw the circle using the
circlefunction. Thecirclefunction 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
drawfunction 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