Cropping the Image

Cropping is the process of selecting only a part of the image. The 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.

Example:

Python3
# Importing Image class from PIL module
from PIL import Image

# Opens a image in RGB mode
im = Image.open(r"geek.jpg")

# Size of the image in pixels 
# (size of original image)
# (This is not mandatory)
width, height = im.size

# Setting the points for cropped image
left = 5
top = height / 4
right = 164
bottom = 3 * height / 4

# Cropped image of above dimension
# (It will not change original image)
im1 = im.crop((left, top, right, bottom))

# Shows the image in image viewer
im1.show()

Output:

Refer to the below articles to get detailed information about Cropping images.

Python Pillow Tutorial

sinceDigital Image processing means processing the image digitally with the help of a computer. Using image processing we can perform operations like enhancing the image, blurring the image, extracting text from images, and many more operations. There are various ways to process images digitally. Here we will discuss the Pillow module of Python. Python Pillow is built on the top of PIL (Python Image Library) and is considered as the fork for the same as PIL has been discontinued since 2011. Pillow supports many image file formats including BMP, PNG, JPEG, and TIFF. The library encourages adding support for newer formats in the library by creating new file decoders.

This article aims at providing information about Python Pillow from basics to advance with the help of well-explained concepts and examples. So, let’s not waste any of the time and dive deep into the Pillow.

Similar Reads

Installation

Python Pillow does not come in-built with Python. To install it type the below command in the terminal....

Opening and Displaying the image

The Pillow module provides the open() and show() function to read and display the image respectively. For displaying the image Pillow first converts the image to a .png format (on Windows OS) and stores it in a temporary buffer and then displays it. Therefore, due to the conversion of the image format to .png some properties of the original image file format might be lost (like animation). Therefore, it is advised to use this method only for test purposes....

Getting information about the opened image

Getting the Size, and format of the Image...

Rotating the Image

rotate() method of the Image class is used to rotate the image by a particular angle counterclockwise around its center. After rotating the image, the sections of the image having no pixel values are filled with black (for non-alpha images) and with completely transparent pixels (for images supporting transparency)....

Flipping the Image

Image.transpose() is used to transpose the image (flip or rotate in 90 degree steps)....

Resizing the image

Image.resize() returns a resized copy of the image. Interpolation happens during the resize process, due to which the quality of image changes whether it is being upscaled (resized to a higher dimension than original) or downscaled (resized to a lower Image then original). Therefore resize() should be used cautiously and while providing suitable value for resampling argument....

Saving the Image

Image.save() saves the image under the given filename. If no format is specified, the format to use is determined from the filename extension, if possible. You can use a file object instead of a filename. In this case, you must always specify the format. The file object must implement the seek, tell, and write methods, and be opened in binary mode....

Merging Images

Image.merge() is used to merge a set of single band images into a new multiband image....

Creating a Thumbnail

Image.thumbnail() convert the image into a thumbnail. This method modifies the image to contain a thumbnail version of itself, no larger than the given size. This method calculates an appropriate thumbnail size to preserve the aspect of the image, calls the draft() method to configure the file reader (where applicable), and finally resizes the image....

Cropping the Image

Cropping is the process of selecting only a part of the image. The crop() method is used to crop a rectangular portion of any image....

Blurring the Image

If a blurred image is observed carefully then a common thing to notice is that image is smooth meaning edges are not observed. A filter used for blurring is also called a low pass filter because it allows the low frequency to enter and stop high frequency. The ImageFilter class in the pillow library provides various filters that can be applied using the filter() method. Let’s see some of the blurring filters provided by the pillow....

Drawing on Images

Pillow provides the ImageDraw module that provides simple 2D graphics for Image objects. You can use this module to create new images, annotate or retouch existing images, and generate graphics on the fly for web use. Let’s see various figures or texts that we can draw on the image....

Enhancing Image

Python Pillow provides the ImageEnhance module to adjust the color, brightness, contrast, and sharpness of the image....

Convert images between various Formats

Convert the .GIF to .BMP and it’s vice-versa in PythonConvert an image into jpg format using Pillow in PythonConvert PNG to JPG using PythonConvert files from jpg to png and vice versa using PythonConvert the .PNG to .GIF and it’s vice-versa in PythonConvert files from jpg to gif and vice versa using PythonConvert OpenCV image to PIL image in Python...

Advance Operations on Image using Pillow

Create transparent png image with Python – PillowAdd padding to the image with Python – PillowFind most used colors in image using PythonPython – Color Inversion using PillowOverlay an image on another image in PythonChange image resolution using Pillow in PythonSpot the difference between two images using PythonHow to Extract Text from Images with Python?Create and save animated GIF with Python – PillowHow to compress images using Python and PIL?Python | Using PIL ImageGrab and PyTesseractFloodfill Image using Python-PillowPython – Channel Drop using Pillow...

Python Pillow Exercises and Applications

Loading Images in Tkinter using PILCreate Certificates using Python-PILPython | OCR on All the Images present in a Folder SimultaneouslyApply changes to all the images in given folder – Using Python PIL...