Skip to content
Related Articles

Related Articles

Improve Article
Wand transform() function in Python
  • Last Updated : 12 Jun, 2020

In order to resize and crop an image at the same time transform() function is used in wand. First crop operation is performed and then resize operation.

Syntax : wand.image.transform(crop=”, resize=”)

Parameters :

ParameterInput TypeDescription
cropbasestringA geometry string defining a subregion of the image to crop to
resizebasestringA geometry string defining the final size of the image

Input Image:

Example #1:



Let us take an image crop it in dimensions 200×200 and then rescale it to 400×400 pixels.




# Import Image from wand.image module
from wand.image import Image
  
# Import display to display final image
from wand.display import display
  
# Read image using Image function
with Image(filename ='koala.jpeg') as img:
  
    # using transform() function
    img.transform('200x200', '200 %')
  
    # Saving image
    img.save(filename ='transform.jpeg')
  
    # display image
    display(img)

Output:

 
Example #2: Let us take an image crop 50 % of all four corners.




# Import Image from wand.image module
from wand.image import Image
  
# Import display to display final image
from wand.display import display
  
# Read image using Image function
with Image(filename ='koala.jpeg') as img:
  
    # using transform() function
    img.transform('50 %')
  
    # Saving image
    img.save(filename ='transform1.jpeg')
  
    # display image
    display(img)

Output:

 
Example #3: Scale height of source image to 200px and preserve aspect ratio.




# Import Image from wand.image module
from wand.image import Image
  
# Import display to display final image
from wand.display import display
  
# Read image using Image function
with Image(filename ='koala.jpeg') as img:
  
    # using transform() function
    img.transform(resize ='x200')
  
    # Saving image
    img.save(filename ='transform3.jpeg')
  
    # display image
    display(img)

Output:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :