Last modified: Apr 12, 2025 By Alexander Williams

Python PIL Image Handling Guide

Python Imaging Library (PIL) is a powerful tool for image processing. It helps open, edit, and save images in various formats. This guide covers the basics of PIL.

What is PIL?

PIL stands for Python Imaging Library. It adds image processing capabilities to Python. The library supports many file formats like JPEG, PNG, and BMP.

PIL is now maintained as Pillow. It is a fork of the original PIL library. Pillow offers better support and updates.

Installing PIL (Pillow)

To use PIL, install the Pillow library. Run this command in your terminal or command prompt.

 
pip install Pillow

Once installed, you can import it in your Python script. Use the PIL module to access its functions.

Opening an Image

Use the Image.open() method to open an image. It returns an image object you can manipulate.

 
from PIL import Image

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

This code opens an image named example.jpg. Ensure the file is in your working directory.

Displaying an Image

Use the show() method to display the image. It opens the image in your default viewer.

 
image.show()

This is useful for quick checks. For more advanced display options, check the Python Imageio Guide.

Getting Image Properties

Images have properties like size and format. Use size to get dimensions and format for the file type.

 
print(image.size)  # Output: (width, height)
print(image.format)  # Output: JPEG, PNG, etc.


(800, 600)
JPEG

For more details on image dimensions, see the Python Image Dimensions Guide.

Resizing an Image

Use the resize() method to change image size. Provide a tuple with new width and height.

 
resized_image = image.resize((400, 300))
resized_image.show()

This reduces the image to half its original size. Always maintain aspect ratio to avoid distortion.

Rotating an Image

Use the rotate() method to turn an image. Specify the angle in degrees.

 
rotated_image = image.rotate(90)
rotated_image.show()

This rotates the image 90 degrees clockwise. Use negative angles for counter-clockwise rotation.

Saving an Image

Use the save() method to store an image. Specify the filename and format.

 
image.save("new_image.png", "PNG")

This saves the image as PNG. You can also convert formats during saving.

Image Filters

PIL provides built-in filters. Import the ImageFilter module to apply effects.

 
from PIL import ImageFilter

blurred_image = image.filter(ImageFilter.BLUR)
blurred_image.show()

This applies a blur effect. Other filters include SHARPEN and CONTOUR.

Working with Pixels

Access pixel data with the load() method. It returns a pixel access object.

 
pixels = image.load()
print(pixels[100, 100])  # Get RGB value at (100, 100)


(255, 0, 0)

For more on pixel manipulation, read the Python Get Image Pixels Guide.

Conclusion

PIL is a versatile library for image handling in Python. It supports opening, editing, and saving images with ease.

This guide covered basic operations. For advanced techniques, explore the Python Image Processing Guide.

Start experimenting with PIL to enhance your images today!