Pillow Tutorial
## Pillow Tutorial
Pillow is the premier library for image processing in Python. It is a friendly, actively maintained fork of the original PIL (Python Imaging Library). Since the original PIL is no longer maintained, Pillow has taken over its role to provide modern Python support and continuous development.
Pillow offers extensive file format support, efficient internal representations, and powerful image processing capabilities.
---
## Who is This Tutorial For?
This tutorial is designed for developers who already have a basic understanding of Python. If you are new to Python, we recommend starting with a foundational Python tutorial first.
---
## Key Features
* **Broad Format Support:** Read and write a wide variety of image formats including JPEG, PNG, GIF, BMP, TIFF, and more.
* **Basic Image Operations:** Easily perform rotation, resizing, cropping, and mirroring.
* **Image Filtering & Enhancement:** Apply built-in filters (blur, sharpen, contour) and adjust brightness, contrast, and color balance.
* **Drawing Capabilities:** Draw shapes, lines, and text directly onto images.
* **Color Space Conversion:** Convert images between different modes such as RGB, L (grayscale), CMYK, and RGBA.
---
## Comparison with Other Libraries
| Library | Focus & Use Case | Comparison with Pillow |
| :--- | :--- | :--- |
| **OpenCV** | Computer vision and real-time video processing. | OpenCV is much more complex. Pillow is simpler, more lightweight, and focused purely on standard image processing. |
| **Matplotlib** | Data visualization and plotting. | Matplotlib is designed for rendering charts and graphs, whereas Pillow is designed for direct image manipulation. |
| **scikit-image** | Scientific image processing and algorithms. | scikit-image is highly academic and scientific. Pillow is more basic, easier to learn, and ideal for everyday image tasks. |
---
## Installation
Before using Pillow, you need to install it via `pip`:
```bash
pip install Pillow
```
*Note: Even though the library is installed as `Pillow`, you will still import it in your code using the namespace `PIL`.*
---
## Your First Pillow Example
The following example demonstrates how to open an image, display its metadata, show it on your screen, and save it in a different format.
### Code Example
```python
from PIL import Image
# Open an image file
img = Image.open("example.jpeg")
# Display the image using the default system viewer
img.show()
# Retrieve and print basic image metadata
print("Image Format:", img.format) # e.g., JPEG, PNG
print("Image Size:", img.size) # Returns a tuple: (width, height)
print("Image Mode:", img.mode) # e.g., RGB, L (grayscale), RGBA
# Save the image in a different format (PNG)
img.save("example.png")
```
### Expected Output
```text
Image Format: JPEG
Image Size: (987, 987)
Image Mode: RGB
```
After running this script, the image will also be converted and saved to your working directory as `example.png`.
---
## Key Considerations & Best Practices
1. **Resource Management:** When processing a large number of images, it is good practice to close the image file explicitly or use a context manager to free up system memory:
```python
with Image.open("example.jpeg") as img:
# Perform image operations here
img.save("output.png")
```
2. **Coordinate System:** Pillow uses a Cartesian pixel coordinate system where `(0, 0)` is the upper-left corner.
3. **In-Place Operations:** Most Pillow operations (like `rotate()` or `resize()`) do not modify the original image object in-place; instead, they return a new `Image` object. Make sure to assign the result to a variable:
```python
# Correct usage
rotated_img = img.rotate(45)
```
---
## Related Resources
* (https://python-pillow.github.io/)
* (https://pillow.readthedocs.io/en/stable/)
* (https://github.com/python-pillow/Pillow)
YouTip