Python Pillow tutorial: Enhance your images with this powerful library

  • Post author:
  • Post comments:0 Comments
  • Reading time:56 mins read

Pillow is an open-source Python library for handling and modifying images. It is built on top of the Python Imaging Library (PIL). Pillow is widely used for tasks such as opening and manipulating image files, creating thumbnails, converting between image formats, applying image filters, and applying image effects.

To install Pillow, you will need to have Python and pip, the Python package manager, installed on your system. Once you have these prerequisites

You can install Pillow using pip with the following command:


pip install pillow

Once Pillow is installed, you can use it in your Python code by importing the PIL module.

Here is an example of how to open an image using Pillow:


from PIL import Image

# Open an image file
with Image.open('image.jpg') as img:
    # Do something with the image
    print(img.format)
    print(img.size)
    print(img.mode)

The Image module contains various methods for manipulating images. Here are a few examples:

  • img.rotate(angle): Rotates the image by the given angle
  • img.resize(size): Resizes the image to the given size
  • img.crop(box): Crops the image to the given box
  • img.save(filename): Saves the image to the given file

Here is an example of how you can use these methods to perform some basic image manipulation:


from PIL import Image

# Open an image file
with Image.open('image.jpg') as img:
    # Rotate the image by 45 degrees
    img = img.rotate(45)
    
    # Resize the image to fit within a 200x200 square
    img.thumbnail((200, 200))
    
    # Crop the image to a square centered on the middle
    width, height = img.size
    left = (width - 200) // 2
    top = (height - 200) // 2
    right = (width + 200) // 2
    bottom = (height + 200) // 2
    img = img.crop((left, top, right, bottom))
    
    # Save the modified image
    img.save('modified_image.jpg')

Here are some examples of how you can use the Pillow module in your Python code:

1) Cropping an image:


from PIL import Image

# Open an image file
image = Image.open('image.jpg')

# Crop the image to a 4x4 region starting at (0, 0)
cropped_image = image.crop((0, 0, 4, 4))

# Display the cropped image
cropped_image.show()

2) Resizing an image:


from PIL import Image

# Open an image file
image = Image.open('image.jpg')

# Resize the image to a width of 300 pixels and maintain the aspect ratio
resized_image = image.resize((300, 300))

# Display the resized image
resized_image.show()

3) Rotating an image:


from PIL import Image

# Open an image file
image = Image.open('image.jpg')

# Rotate the image 90 degrees clockwise
rotated_image = image.rotate(90)

# Display the rotated image
rotated_image.show()

4) Applying a filter to an image:


from PIL import Image, ImageFilter

# Open an image file
image = Image.open('image.jpg')

# Apply the BLUR filter to the image
filtered_image = image.filter(ImageFilter.BLUR)

# Display the filtered image
filtered_image.show()

In conclusion, the Python Pillow library is a powerful tool for handling and modifying images in Python. With its wide range of functions, you can easily open and display images, crop and resize them, rotate them, apply filters and effects, and much more. By following the examples in this tutorial, you should now be able to use Pillow in your own projects to perform a variety of image manipulation tasks. If you have any questions or want to learn more about Pillow, don’t hesitate to ask!

Publisher
Latest posts by Publisher (see all)

Publisher

Publisher @ideasorblogs

Leave a Reply