To use Pillow (Python Imaging Library) in Python, you can follow these steps:
-
Install Pillow:
- Open your terminal or command prompt.
- Run the following command to install Pillow using pip:
pip install pillow
-
Import the Pillow module:
- In your Python script, import the
PIL
module from Pillow:from PIL import Image
- In your Python script, import the
-
Open and manipulate images:
-
Use the
Image.open()
method to open an image file:image = Image.open("image.jpg")
-
Perform various operations on the image, such as resizing, cropping, rotating, and applying filters. Here are a few examples:
# Resize the image resized_image = image.resize((width, height)) # Crop the image cropped_image = image.crop((x, y, x + width, y + height)) # Rotate the image rotated_image = image.rotate(angle) # Apply a filter filtered_image = image.filter(ImageFilter.BLUR)
-
Save the modified image to a file:
resized_image.save("resized_image.jpg")
-
-
Display the image:
-
To display the image, you can use the
show()
method:image.show()
-
This will open the image using the default image viewer on your system.
-
These are just a few examples of what you can do with Pillow. It provides a wide range of image processing capabilities, including image manipulation, format conversion, and more. You can refer to the Pillow documentation for more details and advanced usage: https://pillow.readthedocs.io/