Numerous applications utilize digital images, and with this, there is normally a need to handle the pictures utilized. Assuming you are building your application with Python and need to add image processing features to it, there are different libraries you can utilize. Some common ones are OpenCV, scikit-picture, Python Imaging Library, and Pillow.
In this article, we’ll discuss Pillow. The Python Imaging Library adds image processing capacities to your Python code. This library gives broad file format support, an effective internal representation, and fairly strong image-processing capacities. The core library is intended to access information stored in a few pixel formats quickly. It ought to give a strong groundwork to general image processing tools. Pillow supports a range of image file formats: PNG, JPEG, PPM, GIF, TIFF, and BMP. This article will use Pillow to show how to perform various image operations, such as cropping, resizing, rotating, etc.
Pillow and PIL cannot co-exist in the same environment. So if PIL is installed already, we have to first uninstall it.
Below is the command to install Pillow using pip -
1 2
python3 - m pip install--upgrade pip python3 - m pip install--upgrade Pillow
A pivotal class in the Python Imaging Library is the image class. It’s characterized in the image module and gives a PIL picture on which manipulation operations can be done. An instance of this class can be made in more ways than one: by loading images from a file, making images from scratch, or by processing other images.
We can use the open() method in the image module to load an image from a file, passing it the path to the image.
1 2 3
from PIL import Image im = Image.open("demo.jpg")
If successful, the above code returns an image object. If some issue arises while opening the file, an OSError exception will be raised. After getting an image object, you can use the methods and attributes defined by the class to process and manipulate it.
1 2 3 4 5 6 7 8 9 10 11 12
# To display the image image.show() # The file format of the image file. print(image.format) # Output: JPEG # The pixel format used by the image file.Typical values can be "1", "L", "RGB", or "CMYK." print(image.mode) # Output: RGB # Prints image size, in pixels.The size is given as a tuple(width, height) . print(image.size) # Output: (1920, 1280)
To resize an image, we can use the resize() method on it and pass a two-integer tuple argument consisting of the width and height of the resized image. The method doesn’t modify the used image; rather, it returns another image with the new dimensions.
1
2
3
4
5
6
image = Image.open('demo.jpg')
new_image = image.resize((400, 400))
new_image.save('image_400.jpg')
print(image.size) # Output: (1920, 1280)
print(new_image.size) # Output: (400, 400)
If we want to resize images and keep their aspect ratios, then we can use the thumbnail() function to resize them. This also takes a two-integer tuple argument representing the maximum width and height of the thumbnail.
1
2
3
image.thumbnail((400, 400))
image.save('image_thumbnail.jpg')
print(image.size) # Output: (400, 267)
With the Pillow library, you can crop a picture with the crop() method of the image class. The method takes a box tuple that characterizes the position and size of the cropped area and returns an image object addressing the cropped picture. The coordinates for the box are (left, upper, right, lower). The cropped segment incorporates the left column and the upper row of pixels and goes up to (however, does exclude) the right column and base row of pixels. Let’s see an example -
1
2
3
4
5
6
box = (200, 300, 700, 600)
cropped_image = image.crop(box)
cropped_image.save('cropped_image.jpg')
# Print size of cropped image
print(cropped_image.size) # Output: (500, 300)
Using the rotate() function, we can rotate our images. This takes an integer or float argument representing the degrees to rotate an image and returns a new image object of the rotated image. The rotation is done counterclockwise.
1
2
3
4
5
image_rot_90 = image.rotate(90)
image_rot_90.save('image_rot_90.jpg')
image_rot_180 = image.rotate(180)
image_rot_180.save('image_rot_180.jpg')
In this article, we discussed some aspects of the Pillow library. If you want to find out more, be sure to read the documentation. We’ll discuss something new in our next article. Until then, happy coding :)