Python Wand libraryImageMagick is a utility designed and developed for the image conversion from one format to another. The tool has been supported by a large community because of a versatile variety of image formats and its precise as well as straightforward execution. We can get images from PDF format files. In the following tutorial, we will be going to discuss a binding developed for Python by Imagemagick called wand. We will understand the features and uses of the Wand library in the Python programming language with different examples. So, let’s get begun. Understanding the Wand library in PythonThe wand is an ImageMagick library developed for the Python programming language. This library supports the functionalities of ImageMagick or Imagick API in Python version 2.6, 2.7, 3.3+, and PyPy. The wand is used to open and manipulate images. This library not only supports the processing of the images but also offers valuable functionalities for Machine Learning codes with the help of the NumPy library. The wand provides a big number of functions to manipulate images. Some of the uses of the Python wand library are shown below: Uses of the Python Wand library:
Now, let us install the Python wand library. How to install the wand library?We can install the wand module with the help of the pip installer using the following command: Syntax: The module will be installed in the system as the version of Python and pip. As we have discussed that the wand library is an Imagick API, thus, we have to include the Imagick dependencies also. The installation procedure for Imagick dependencies is quite distinct for different Operating Systems. For Ubuntu/Debian:Syntax: For Mac (Using Brew Installer):Syntax: Installation of MacPort:Syntax: Note: If Python is not installed using MacPort, we have to export MAGICK_HOME as shown below:Syntax: For Windows:We can create ImageMagick by ourselves; however, it needs a build tool chain such as Visual Studio in order to compile it. One simple method is to download a prebuilt binary of ImageMagick for the architecture (win32 or win64) We can download it from the following links: Verifying the InstallationIn order to check whether the module has been installed in the system properly or not, we can try importing the module and execute the program. Once the installation is complete, create a new Python file and type the following syntax in it. Example: Now, save the file and run the file using the following command in the command prompt. Syntax: If the program runs without raising any import error, the module is installed properly. Else it is recommended to reinstall the module and refer to its official documentation. Now, let us understand start working with the wand library. Reading an Image using Python WandThe image library of the Python wand allows the programmers to import the image file using its Image module. We can then use different attributes in order to read the image, such as height, width, and many more. Let us consider the following example for the same. Example: Input Image: Output: Height of the image: 33 Width of the image: 150 Explanation: In the above snippet of code, we have imported the Image module from the wand.image library. We have then used the Image() function to import the image from the directory. We have then used different attributes to read the dimensions of the input image. As a result, the program returned the dimensions (height and width) of the images to the users. Blurring an Image using Python WandThe wand library allows the programmers in order to blur an image using the blur() function and save it as the save() function. Let us consider the following example to understand the same. Example: Input Image: Output: Explanation: In the above snippet of code, we have imported the Image module from the wand.image library. We have then imported the required image from the directory and used the blur() function, specifying the radius parameter to 0 and the sigma parameter to 3. At last, we have saved the file using the save() function specifying the new filename to the image file. As a result, the provided image gets blurred with radius 0 and sigma 3 and saved as the specified filename. Transforming the image using Python WandWe can also transform the image using the several functions of the wand library. Let us consider the following example to understand the same. Example: Input Image: Output: Explanation: In the above snippet of code, we have imported the Image module from the wand.image library. We have then again imported the image file from the directory and used the clone() function to make the image. We have then used the flip() function on the cloned image and saved the resultant image in the file using the save() function. As a result, the input image has been transformed successfully. Drawing different figures using Python WandThe Python wand library also provides us with different functions that allow us to draw different figures. Let us consider the following example to understand the same. Example: Output: Explanation: In the above snippet of code, we have imported the required modules from the wand library. We have then used the Drawing() function to draw a circle. We have then used different attributes specifying the dimensions, color, and outline color. We have then used the Image() function specifying the height, width, and background color and saved the resultant image for the users using the save() function. |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263353.html