Python PIL | Image.crop() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. PIL.Image.crop() method is used to crop a rectangular portion of any image.
Syntax: PIL.Image.crop(box = None)
Parameters:
box – a 4-tuple defining the left, upper, right, and lower pixel coordinate.
Return type: Image (Returns a rectangular region as (left, upper, right, lower)-tuple).
Return: An Image object.
Code #1:
# Improting Image class from PIL module from PIL import Image # Opens a image in RGB mode im = Image.open(r"C:\Users\Admin\Pictures\geeks.png") # Size of the image in pixels (size of orginal image) # (This is not mandatory) width, height = im.size # Setting the points for cropped image left = 5top = height / 4right = 164bottom = 3 * height / 4 # Cropped image of above dimension # (It will not change orginal image) im1 = im.crop((left, top, right, bottom)) # Shows the image in image viewer im1.show() |
Original Image

Output:

Code #2:
# Improting Image class from PIL module from PIL import Image # Opens a image in RGB mode im = Image.open(r"C:\Users\Admin\Pictures\network.png") # Setting the points for cropped image left = 155top = 65right = 360bottom = 270 # Cropped image of above dimension # (It will not change orginal image) im1 = im.crop((left, top, right, bottom)) # Shows the image in image viewer im1.show() |
Original Image:

Output:

Recommended Posts:
- PHP | imagecrop() Function
- class method vs static method in Python
- Python | next() method
- Python | os.dup() method
- Python | set() method
- Python | sympy.div() method
- Python | os.get_terminal_size() method
- Python | os.mknod() method
- Python | os.ftruncate() method
- Python | os.truncate() method
- Python PIL | getpalette() Method
- Python | sympy.gcd() method
- Python | Tensorflow exp() method
- Python | sympy.lcm() method
- Python | os.chmod method
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.



