Wand transform() function in Python


The transform() function in the Wand library of Python is used to apply various transformations to an image. It takes in a set of arguments that define the type of transformation to be applied and returns the transformed image.

The syntax for the transform() function is as follows:

transform(self, affines=None, matrix=None, crop=None, background=None, filter=None, blur=None, sharpen=None, brightness=None, contrast=None, noise=None, channel=None, colorspace=None, alpha=None, format=None)

Here is a brief explanation of the arguments that can be passed to the transform() function:

  • affines: A list of affine transformation matrices to be applied to the image.
  • matrix: A transformation matrix to be applied to the image.
  • crop: A tuple of the form (width, height, x, y) that defines the region of the image to be cropped.
  • background: The background color to be used for the transformed image.
  • filter: The type of filter to be used for the transformation.
  • blur: The amount of blur to be applied to the image.
  • sharpen: The amount of sharpening to be applied to the image.
  • brightness: The amount of brightness adjustment to be applied to the image.
  • contrast: The amount of contrast adjustment to be applied to the image.
  • noise: The type of noise to be added to the image.
  • channel: The channel(s) to be modified in the image.
  • colorspace: The colorspace to be used for the transformed image.
  • alpha: The alpha channel to be used for the transformed image.
  • format: The format of the transformed image.

Here are some examples of using the transform() function:

from wand.image import Image

# Open an image
with Image(filename='input.jpg') as img:
    # Rotate the image by 90 degrees
    img.transform(affines=['90'])

    # Crop the image to a region of 100x100 pixels starting at (50, 50)
    img.transform(crop=(100, 100, 50, 50))

    # Add Gaussian blur to the image
    img.transform(blur=(0, 10))

    # Adjust the brightness of the image
    img.transform(brightness=0.5)

    # Convert the image to grayscale
    img.transform(colorspace='gray')

    # Save the transformed image
    img.save(filename='output.jpg')


About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.