Pillow is a third-party library that supports various image file formats such as JPEG, PNG, PPM, GIF, BMP, and TIFF.
The pillow library supports operations like resizing, cropping, adding text to images, rotating, and greyscaling.
How to Crop Image in Python
To crop an image in Python, use the pillow library’s Image.crop() method. When the image is cropped, a rectangular region inside the image is selected and retained while everything else outside the area is removed.
With a Pillow library, you can crop an image with the crop() method of the Image class.
Syntax
Image.crop(box=None)
Parameters
box: The crop rectangle, as a (left, upper, right, lower)-tuple.
Return Value
The Image.crop() method returns a rectangular region from the provided image. The box is a four-tuple defining the left, upper, right, and lower pixel coordinates.
Steps to crop image in Python
To crop an image in Python, you can use a pillow imaging library and follow the below steps.
- Install Pillow.
- Import the Image class from PIL and open the image.
- Crop the image using the crop() method.
Step 1: Install Pillow
Before installing the Pillow library, some prerequisites must be fulfilled. These vary for different operating systems.
We won’t list the different options here, and you can find the postulate for your specific OS in the installation guide.
After installing the required libraries, you can install Pillow with pip
.
Type the following command.
python3 -m pip install pillow # OR pip install pillow
PIL is the Python Imaging Library that provides a python interpreter with image editing capabilities.
The Image module provides the class with the same name used to represent the PIL image. The module also offers several factory functions, including loading images from files and creating new images.
Step 2: Import PIL and open the image
To import the Image class from PIL, use the following code.
from PIL import Image
The Image Object
An essential class in the Python Imaging Library is the Image class.
It is defined under the Image module and supports a PIL image on which editing operations can be carried out. An instance(object) of the Image class can be created in one of the following ways:
- By loading images from the file,
- Creating images from scratch.
- As a result of processing other images.
To load the image from a file system, we use the open() method from the Image module and pass the path to the image.
See the following code.
from PIL import Image try: imgPath = './forest.jpg' img = Image.open(imgPath) img.show() except FileNotFoundError: print('Provided image path is not found')
Output
After obtaining the Image object, you can now use the methods and attributes defined by the class to process and manipulate it.
You can show the image by calling the show() method on the obtained object. The show() method displays the image on the external viewer. For example, on the macOS, it opens on preview software.
If the path we have provided is incorrect, it will throw a FileNotFoundError exception. We have handled that exception in our code by printing the message in the console.
Now, we will crop this image using the crop() function.
Step 3: Crop the image using the crop() method.
When the image is cropped, a rectangular region inside the image is selected and retained while everything else outside the zone is removed.
With the Pillow library, you can crop the image with the crop() method of the Image class. The method takes a box tuple as an argument that defines the position and size of the cropped region and returns the Image object representing the cropped image.
The Python Imaging Library uses the coordinate system that starts with (0, 0) in the upper left corner. The first two values of a box tuple define the upper left starting position of the crop box.
The third and fourth values define the distance in pixels from this starting position towards the right and bottom direction, respectively.
The right can also be represented as (left+width), and the lower can be described as (upper+height).
The crop method from an Image module takes four coordinates as input. The coordinates for the box are (left, upper, right, and lower).
The cropped section consists of the left column and the upper row of pixels and goes up to (but doesn’t consist) the right column and bottom row of pixels. This is better explained with the above example. Let’s see an example with an image.
My image for this example is somewhat big, but yours may be small. So write the coordinates according to your image size and crop requirement.
See the following code.
from PIL import Image try: imgPath = './forest.jpg' img = Image.open(imgPath) img.show() # Crop the image using crop() method box = (2000, 2500, 4000, 4000) croppedImage = img.crop(box) croppedImage.save('cropped_image.jpg') croppedImage.show() except FileNotFoundError: print('Provided image path is not found')
Output
This image is cropped part from the original image.
I have written box = (2000, 2500, 4000, 4000) in this example.
So that means (left, upper, right, lower) = (2000, 2500, 4000, 4000).
So, according to the above coordinates, it will crop the image.
Conclusion
In this example, we have seen the following points.
- Install the Pillow and import the Image module from the PIL module.
- Then we see the Image objects and attributes.
- Next, we have seen how to crop the image using the crop() function.
That’s it.