Introduction
PIL, which stands for Python Imaging Library, is a powerful tool for working with images in Python. It provides a wide range of functionalities to manipulate, process, and analyze digital images. With PIL, you can easily open, edit, and save various image file formats.

In this article, we will discuss the basic operations you can perform with the help of the PIL library, like opening images, displaying them, obtaining image information, rotating, resizing, and saving images.
Beginning with Pillow
To start using PIL, you first need to install the library. You can do this by running the following command in your terminal or command prompt:
pip install Pillow
Once the installation is complete, you can import the PIL module in your Python script using the following line:
from PIL import Image
This line imports the `Image` module from the PIL library, which provides the necessary functions and classes for image manipulation.
1. Opening an image using open()
To work with an image using PIL, you need to open it first. PIL provides the `open()` function to load an image from a file. Here's an example of how to open an image:
from PIL import Image
image = Image.open("image.jpg")
In this code snippet, we use `Image.open()` to open the image file named "image.jpg". The `open()` function takes the file path as an argument and returns an `Image` object representing the opened image.
It's important to note that the image file should be located in the same directory as your Python script or you should provide the full path to the image file.
2. Displaying the image using show()
After opening an image, you might want to display it to see what it looks like. PIL provides the `show()` method to display the image in your default image viewer. Here's an example:
from PIL import Image
image = Image.open("image.jpg")
image.show()
In this code, after opening the image using `Image.open()`, we call the `show()` method on the `image` object. This will open the image in your default image viewer, allowing you to see the image.
Note that the `show()` method is useful for quickly visualizing the image, but it may not work in all environments, such as when running the code on a server or in a non-interactive mode.
3. Obtaining information about the opened image
Once you have an image opened, you can retrieve various information about it using the properties and methods provided by PIL. Let's explore a few common ways to obtain image information.
A) Getting the mode (color mode) of the image
The mode of an image represents the color format and depth of the image. You can retrieve the mode of an image using the `mode` attribute.
For example
from PIL import Image
image = Image.open("image.jpg")
print("Image mode:", image.mode)
This code will print the mode of the opened image. Common modes include "RGB" (Red, Green, Blue) for color images, "L" for grayscale images, and "RGBA" for images with transparency.
B) Getting the size of the image
To get the size of an image (width and height in pixels), you can use the `size` attribute.
For example :
from PIL import Image
image = Image.open("image.jpg")
print("Image size:", image.size)
This code will print the size of the image as a tuple in the format (width, height).
C) Getting the format of the image
PIL can determine the file format of an image using the `format` attribute.
For example :
from PIL import Image
image = Image.open("image.jpg")
print("Image format:", image.format)
This code will print the format of the image, such as "JPEG", "PNG", or "GIF".
4. Rotating an image using rotate()
PIL allows you to rotate an image by a specified angle using the `rotate()` method. Here's an example of how to rotate an image:
from PIL import Image
image = Image.open("image.jpg")
rotated_image = image.rotate(45)
rotated_image.show()
In this code, we first open the image using `Image.open()`. Then, we call the `rotate()` method on the `image` object, passing the desired rotation angle as an argument. In this case, we rotate the image by 45 degrees clockwise.
The `rotate()` method returns a new `Image` object with the rotated image. We assign this rotated image to the variable `rotated_image` and then call the `show()` method to display the rotated image.
You can specify any angle for rotation, positive or negative. Positive angles rotate the image clockwise, while negative angles rotate it counterclockwise.
Note that rotating an image may result in a larger dimension which is compared to the original image, as the rotated image needs to accommodate the entire rotated content.
5. Resizing an image using resize()
PIL provides the `resize()` method to change the size of an image. You can specify the new dimensions (width and height) of the image using this method.
For example :
from PIL import Image
image = Image.open("image.jpg")
new_size = (800, 600)
resized_image = image.resize(new_size)
resized_image.show()
In this code, we open the image using `Image.open()`. Then, we define the new size we want for the image as a tuple `(width, height)`. In this example, we set the new size to (800, 600) pixels.
We call the `resize()` method on the `image` object, passing the `new_size` tuple as an argument. The `resize()` method returns a new `Image` object with the resized image.
We assign the resized image to the variable `resized_image` and then call the `show()` method to display the resized image.
It's important to note that resizing an image may affect its aspect ratio if the new dimensions are not proportional to the original dimensions. PIL provides additional options to control the resizing behavior, such as preserving the aspect ratio or using different resampling filters.
6. Saving an image using save()
After performing various operations on an image, you may want to save the modified image to a file. PIL allows you to save an image using the `save()` method.
For example :
from PIL import Image
image = Image.open("image.jpg")
# Perform operations on the image
image.save("modified_image.jpg")
In this code, we open an image using `Image.open()`. After performing any desired operations on the image, we call the `save()` method on the `image` object to save the modified image to a file.
The `save()` method takes the file path and format as arguments. In this example, we save the modified image as "modified_image.jpg". PIL will automatically determine the image format based on the file extension provided.
You can specify various file formats when saving an image, such as JPEG, PNG, GIF, BMP, etc. PIL supports saving images in multiple formats, depending on the capabilities of the installed library.
It's important to choose an appropriate file format based on the image content and the desired quality. For example, JPEG is commonly used for photographs, while PNG is suitable for images with transparency or graphics.
Note: Remember to provide a valid file path and ensure that you have write permissions in the specified directory.



