Getting Started with Pillow (PIL) for Image Processing on macOS Tahoe
Pillow is a powerful and easy-to-use Python library for image processing. It is a fork of the original PIL (Python Imaging Library) and provides a wide range of functionalities for image manipulation, analysis, and format conversion.
Prerequisites
- Python 3 installed (recommended to use Miniconda or Homebrew).
- A virtual environment created using
venv(see Creating Python Virtual Environments with venv on macOS Tahoe).
Installation
-
Activate your virtual environment:
source .venv/bin/activate -
Install Pillow using pip:
pip install pillow
Basic Usage
Here are a few basic examples of using Pillow:
1. Opening an Image
from PIL import Image
img = Image.open("my_image.jpg")
img.show() # Opens the image in your default image viewer
2. Resizing an Image
from PIL import Image
img = Image.open("my_image.jpg")
new_img = img.resize((200, 200)) # Resize to 200x200 pixels
new_img.save("my_image_resized.jpg")
3. Converting Image Format
from PIL import Image
img = Image.open("my_image.png")
img.convert('RGB').save("my_image.jpg") # Convert to JPEG
4. Cropping an Image
from PIL import Image
img = Image.open("my_image.jpg")
cropped_img = img.crop((100, 100, 300, 300)) # (left, upper, right, lower)
cropped_img.save("my_image_cropped.jpg")
Related Guides
- Creating Python Virtual Environments with venv on macOS Tahoe
- Installing Python Packages with Pip Without Sudo on macOS Tahoe