PIL Image Basics
The PIL (Python Imaging Library) — now maintained as Pillow — lets you load, create, resize, crop, and save images directly in Python.
Learning Objectives
- 12.6.2.2 Use commands of module Image in PIL library (load, create, size, save) to manipulate images
Conceptual Anchor
The Photo Lab Analogy
PIL is your digital photo lab. Image.open() = taking a photo from
the drawer. Image.new() = getting a blank canvas. .size = measuring
the print. .save() = printing and filing the photo. .crop() = cutting
out a section.
Rules & Theory
Installing Pillow
# Install via pip (only needed once)
pip install Pillow
# Import in Python
from PIL import ImageCore Image Commands
| Command | Description | Example |
|---|---|---|
Image.open(path) |
Load an existing image from file | img = Image.open("photo.jpg") |
Image.new(mode, size, color) |
Create a new blank image | img = Image.new("RGB", (400,300), (255,0,0)) |
img.size |
Get image dimensions as (width, height) | w, h = img.size |
img.save(path) |
Save image to file | img.save("output.png") |
img.show() |
Display image in default viewer | img.show() |
img.resize((w, h)) |
Resize image (returns new image) | small = img.resize((200, 150)) |
img.crop((left, top, right, bottom)) |
Cut out a rectangular region | face = img.crop((50,50,200,200)) |
img.rotate(angle) |
Rotate image by degrees | rotated = img.rotate(90) |
img.convert(mode) |
Convert color mode | gray = img.convert("L") |
Image Modes
| Mode | Description | Channels |
|---|---|---|
"RGB" |
Full color (Red, Green, Blue) | 3 channels, 0–255 each |
"RGBA" |
Color + transparency (Alpha) | 4 channels |
"L" |
Grayscale | 1 channel, 0 (black) – 255 (white) |
"1" |
Black and white (binary) | 1 channel, 0 or 255 |
Worked Examples
1 Load, Resize, and Save
from PIL import Image
# Load an image
img = Image.open("landscape.jpg")
print(f"Original size: {img.size}") # e.g., (1920, 1080)
# Resize to thumbnail
thumbnail = img.resize((320, 180))
print(f"Thumbnail size: {thumbnail.size}")
# Save as PNG
thumbnail.save("landscape_thumb.png")2 Create a New Image
from PIL import Image
# Create a 400×300 red image
red_img = Image.new("RGB", (400, 300), (255, 0, 0))
red_img.save("red_canvas.png")
# Create with gradient (pixel-by-pixel)
img = Image.new("RGB", (256, 256))
for x in range(256):
for y in range(256):
img.putpixel((x, y), (x, y, 128))
img.save("gradient.png")3 Pixel Manipulation
from PIL import Image
img = Image.open("photo.jpg")
w, h = img.size
# Get pixel at (10, 20)
pixel = img.getpixel((10, 20))
print(pixel) # e.g., (142, 87, 201)
# Set pixel to white
img.putpixel((10, 20), (255, 255, 255))
# Invert all pixels
for x in range(w):
for y in range(h):
r, g, b = img.getpixel((x, y))
img.putpixel((x, y), (255-r, 255-g, 255-b))
img.save("inverted.png")Common Pitfalls
resize() Creates a NEW Image
img.resize() returns a new image — it doesn't modify the original.
Always assign the result: small = img.resize((200, 100)).
Coordinate System
PIL uses (x, y) with the origin at top-left. x increases right, y increases downward. This is opposite to math convention!
Tasks
Write a program that creates a 500×500 image with a blue background and saves it as "canvas.png".
Write a program that loads any image, converts it to grayscale, resizes it to 200×200, and saves the result.
Write a program that swaps the red and blue channels of every pixel in an image.
Self-Check Quiz
Q1: How do you create a new 800×600 green image?
img = Image.new("RGB", (800, 600), (0, 255, 0))Q2: What does img.size return?
Q3: What mode would you use for a transparent image?