Pillow vs PIL: What’s Changed and What You Need to Know

The Python Imaging Library (PIL) has long been a staple for image processing in Python. However, PIL’s development stagnated, and a fork named Pillow emerged. Pillow has since become the de - facto standard for image processing in Python, offering a modern and well - maintained alternative to PIL. In this blog post, we’ll explore the key differences between PIL and Pillow, typical usage scenarios, common pitfalls, and best practices.

Table of Contents

  1. Core Concepts
  2. Typical Usage Scenarios
  3. Common Pitfalls
  4. Best Practices
  5. Conclusion
  6. References

Core Concepts

PIL (Python Imaging Library)

PIL was one of the earliest libraries for image processing in Python. It provided a wide range of image manipulation functions, including opening, resizing, cropping, and converting images. However, its last official release was in 2009, and it has limited support for modern Python versions and operating systems.

Pillow

Pillow is a friendly fork of PIL. It maintains backward compatibility with PIL while adding new features, improving performance, and providing better support for modern Python and operating systems. Pillow offers a more extensive set of image filters, support for additional image formats, and better error handling.

Typical Usage Scenarios

Opening and Displaying an Image

# Using Pillow
from PIL import Image

try:
    # Open an image file
    img = Image.open('example.jpg')
    # Display the image
    img.show()
except FileNotFoundError:
    print("The image file was not found.")

In PIL, the code would be almost identical. The main difference is that Pillow is more likely to work with modern Python versions and handle errors more gracefully.

Resizing an Image

from PIL import Image

# Open the image
img = Image.open('example.jpg')

# Resize the image
new_size = (200, 200)
resized_img = img.resize(new_size)

# Save the resized image
resized_img.save('resized_example.jpg')

Pillow provides additional interpolation methods for resizing, such as Image.LANCZOS, which can result in higher - quality resized images compared to PIL.

Converting Image Formats

from PIL import Image

# Open a PNG image
img = Image.open('example.png')

# Convert it to JPEG and save
img = img.convert('RGB')
img.save('example.jpg')

Pillow supports a wider range of image formats, including WebP, which was not well - supported in PIL.

Common Pitfalls

Compatibility Issues

If you have existing code written for PIL, you may encounter compatibility issues when switching to Pillow. However, most of the basic functions are the same, so the changes are usually minimal.

Memory Management

Both PIL and Pillow can consume a significant amount of memory when working with large images. It’s important to close images explicitly using the close() method to free up memory.

from PIL import Image

img = Image.open('large_image.jpg')
# Do some processing
img.close()

File Encoding and Permissions

When saving images, issues can arise due to incorrect file encoding or insufficient permissions. Pillow will raise appropriate exceptions, but it’s still important to handle these cases in your code.

Best Practices

Use Pillow Instead of PIL

Given that PIL is no longer actively maintained, it’s recommended to use Pillow for all new projects. If you have an existing project using PIL, consider migrating to Pillow for better support and new features.

Error Handling

Always use try - except blocks when working with images. This helps to handle errors such as file not found, incorrect image format, or insufficient memory.

Performance Optimization

For performance - critical applications, use Pillow’s built - in optimization features. For example, use the appropriate interpolation method when resizing images and avoid unnecessary image conversions.

Conclusion

In summary, Pillow is a significant improvement over PIL. It offers better compatibility with modern Python versions, a wider range of image formats, and improved performance and error handling. While there are some minor compatibility issues when migrating from PIL to Pillow, the benefits far outweigh the drawbacks. By following the best practices and being aware of the common pitfalls, you can effectively use Pillow for all your image processing needs.

References