Skip to content
Related Articles

Related Articles

Wand crop() function in Python
  • Last Updated : 22 Apr, 2020

Cropping an image refers to select an area of image and discard everything outside the cropped area. Crop tool is an important tool as it allows us to get the only relevant part of an image. Also sometimes something unwanted may be contained in the image and that can be discarded from the image using Crop tool.

Image Cropping can be performed using two methods :

  • Using crop() method
  • Using Slicing Operator

Using crop() method –

crop() is an in-built method present in Wand library specially to perform crop operation on Image. Lets get forward towards Parameters in crop() function.

Syntax :




wand.image.crop(left, top, right, bottom, width, 
                  height, reset_coords, gravity)
  
# width and right parameter are exclusive each other
# height and bottom parameter are exclusive each other

Parameters :



ParameterInput TypeDescription
leftnumbers.Integralx-offset of the cropped image. default is 0
topnumbers.Integraly-offset of the cropped image. default is 0
rightnumbers.Integralsecond x-offset of the cropped image. default is 0
bottomnumbers.Integralsecond y-offset of the cropped image. default is 0
widthnumbers.Integralthe width of the cropped image. default is the width of the image. this parameter and right parameter are exclusive each other.
heightnumbers.Integralthe height of the cropped image. default is the height of the image. this parameter and bottom parameter are exclusive each other.
reset_ coordsbooloptional flag. If set, after the rotation, the coordinate frame will be relocated to the upper-left corner of the new image. By default is True.
gravityGRAVITY_TYPESOptional flag. If set, will calculate the top and left attributes. This requires both width and height parameters to be included.

Input Image –




# import Image from wand.image
from wand.image import Image
from wand.display import display
  
# read image using Image() function
with Image(filename = 'gog.png') as img:
  
    # crop image using crop() function
    img.crop(50, 50, 190, 170)
  
    # save resized image
    img.save(filename = 'croped_gog.png')
    display(img)

Output:

Using slicing operator –

Another way to perform crop operation is using slice operator.You can crop an image by [left:right, top:bottom] with maintaining the original. Slice operator is used with the original read file.

Syntax :




with Image(filename = 'filename.format') as img:
    with img[left:right, top:bottom]  as cropimg:
    # other manipulation

Input Image :




# import Image from wand.image
from wand.image import Image
from wand.display import display
  
# read image using Image() function
with Image(filename = 'koala.jpeg') as img:
  
     # cropping image using spliing operator
     with img[100:250, 120:250] as crpimg
         crpimg.save(filename ='crpimg.jpg')
  
         # display image
         display(crpimg)

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 :