motion_blur() in Python Wand


In the given problem statement we have to apply a motion_blur function for an image using the Python wand library. Sometimes we are needed to perform the blur operation on some images. So using the Python wand we can do this task easily and blur the image to any extent.

What is Python Wand?

The wand is a tool, which is used to convert the format of images. It can convert the image from one format to another. This library provides a wide range of functions to perform different kinds of operations of the image for manipulating them. To install the Wand library in our system we can use this command pip install wand.

The usage of Wand library are −

  • It can convert the images from one form to another form

  • With the help of Wand we can read or write images of any format.

  • This library can be used to scale and crop the images as well.

  • The color enhancements of the images can also be done using Wand.

  • This library can be useful to transform images.

  • We can also add simple and special effects to images.

Syntax

So the motion_blur function is the built-in function in the Wand library, which is helpful to reproduce or manipulate the given image. The syntax or motion_blur is as below −

motion_blur(radius, sigma, angle)

Logic for The Above Problem

To solve the given problem we will use the wand library and use the motion_blur function to blur the image as per the given radius, sigma and angle.

Algorithm

  • Step 1 − First import the Image function from the Wand library.

  • Step 2 − Define the function to perform the given task and name it as apply_motion_blur inside this function we will pass two parameters: img_loc, which is the location of the input image and blur_img_loc which is the location of the blur image or output image.

  • Step 3 − Next we will call the input image to perform the blur operation.

  • Step 4 − Then we will make a clone of the input image using the clone function and save the cloned image as image_motion_blur.

  • Step 5 − Now we will apply the motion_blur function of the Image_motion_blur image and give the values of radius, sigma and angle.

  • Step 6 − After applying the motion_bllur function we will save the blurred image in the specified location.

Example

# import the Image function from wand library
from wand.image import Image

def apply_motion_blur(img_loc, blur_img_loc):

   #Call the image to perform blur functionality
   with Image(filename=img_loc) as img:
   
      # make a copy of the image
      with img.clone() as image_motion_blur:
      
         # call the motion_blur function
         image_motion_blur.motion_blur(
            27, 4, 50)  # values of radius, sigma and angle
         image_motion_blur.save(
            filename=blur_img_loc
         )  #store the image with image_motion_blur name


#testing the function
img_location = 'H:/Python/tutorialspointimage.jpg'
blur_img_location = 'image_motion_blur.jpg'

apply_motion_blur(img_location, blur_img_location)

Output

$$\mathrm{Before \: motion \: blur}$$

$$\mathrm{After \: motion \: blur}$$

Complexity

The time complexity for the above created algorithm is O(1), as we have cloned the image and performed the blur operation which takes constant time to complete the execution.

Conclusion

As we have successfully implemented the code for performing and showing the working mechanism of motion_blur function in Python Wand. With the help of this function we have efficiently converted the input image in the blur image as per the given radius, sigma and angle.

Updated on: 16-Oct-2023

19 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements