Wand crop() function in Python

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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Parameters :



Parameter Input Type Description
left numbers.Integral x-offset of the cropped image. default is 0
top numbers.Integral y-offset of the cropped image. default is 0
right numbers.Integral second x-offset of the cropped image. default is 0
bottom numbers.Integral second y-offset of the cropped image. default is 0
width numbers.Integral the width of the cropped image. default is the width of the image. this parameter and right parameter are exclusive each other.
height numbers.Integral the height of the cropped image. default is the height of the image. this parameter and bottom parameter are exclusive each other.
reset_ coords bool optional 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.
gravity GRAVITY_TYPES Optional flag. If set, will calculate the top and left attributes. This requires both width and height parameters to be included.

Input Image –

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Input Image :

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


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.




My Personal Notes arrow_drop_up

Hi there I am a student of CGC COE, Landran in CSE department and have a great interest in programming

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.