Pillow Image Module
Image is the core module of the Pillow library, providing basic image processing functionality.
The following are the most commonly used methods in the Image module and their functional descriptions:
| Method | Description | Example |
|---|---|---|
Image.open(fp, mode='r') |
Open and identify an image file | img = Image.open('image.jpg') |
Image.new(mode, size, color=0) |
Create a new image | new_img = Image.new('RGB', (500, 500), 'blue') |
Image.save(fp, format=None, **params) |
Save an image | img.save('output.png') |
Image.show(title=None, command=None) |
Display an image | img.show() |
Image.resize(size, resample=None, box=None) |
Resize an image | img.resize((200, 200)) |
Image.rotate(angle, resample=0, expand=0) |
Rotate an image | img.rotate(45) |
Image.crop(box=None) |
Crop an image | img.crop((100, 100, 400, 400)) |
Image.paste(im, box=None, mask=None) |
Paste an image | img.paste(logo, (50, 50)) |
Image.convert(mode, matrix=None, dither=None) |
Convert image mode | img.convert('L') |
Image.filter(filter) |
Apply filters | img.filter(ImageFilter.BLUR) |
Image.copy() |
Copy an image | copy = img.copy() |
Image.thumbnail(size) |
Create a thumbnail | img.thumbnail((100, 100)) |
Image.getpixel(xy) |
Get pixel value | pixel = img.getpixel((50, 50)) |
Image.putpixel(xy, value) |
Set pixel value | img.putpixel((50, 50), (255, 0, 0)) |
Image.split() |
Split channels | r, g, b = img.split() |
Image.merge(mode, bands) |
Merge channels | Image.merge('RGB', (r, g, b)) |
3. Common Attributes
In addition to methods, Image objects also have some important attributes:
| Attribute | Description | Example |
|---|---|---|
format |
Image format | print(img.format) |
size |
Image size (width, height) | print(img.size) |
width |
Image width | print(img.width) |
height |
Image height | print(img.height) |
mode |
Image mode | print(img.mode) |
info |
Image-related information dictionary | print(img.info) |
Practical Application Examples
Basic Image Operations
Example
from PIL import Image
# Open image
img = Image.open('example.jpg')
# Display image information
print(f"Format: {img.format}, Size: {img.size}, Mode: {img.mode}")
# Resize and save
resized_img = img.resize((300, 300))
resized_img.save('resized_example.jpg')
# Convert to grayscale
gray_img = img.convert('L')
gray_img.save('gray_example.jpg')
# Open image
img = Image.open('example.jpg')
# Display image information
print(f"Format: {img.format}, Size: {img.size}, Mode: {img.mode}")
# Resize and save
resized_img = img.resize((300, 300))
resized_img.save('resized_example.jpg')
# Convert to grayscale
gray_img = img.convert('L')
gray_img.save('gray_example.jpg')
Image Processing
Example
from PIL import Image, ImageFilter
img = Image.open('example.jpg')
# Apply blur filter
blurred = img.filter(ImageFilter.BLUR)
blurred.save('blurred.jpg')
# Apply contour filter
contour = img.filter(ImageFilter.CONTOUR)
contour.save('contour.jpg')
# Rotate image
rotated = img.rotate(45, expand=True)
rotated.save('rotated.jpg')
img = Image.open('example.jpg')
# Apply blur filter
blurred = img.filter(ImageFilter.BLUR)
blurred.save('blurred.jpg')
# Apply contour filter
contour = img.filter(ImageFilter.CONTOUR)
contour.save('contour.jpg')
# Rotate image
rotated = img.rotate(45, expand=True)
rotated.save('rotated.jpg')
Advanced Features
Image Compositing
Example
from PIL import Image
# Open two images
img1 = Image.open('background.jpg')
img2 = Image.open('foreground.png')
# Ensure both images have the same size
img2 = img2.resize(img1.size)
# Composite images (assuming foreground.png has an alpha channel)
composite = Image.alpha_composite(img1.convert('RGBA'), img2.convert('RGBA'))
composite.save('composite.png')
# Open two images
img1 = Image.open('background.jpg')
img2 = Image.open('foreground.png')
# Ensure both images have the same size
img2 = img2.resize(img1.size)
# Composite images (assuming foreground.png has an alpha channel)
composite = Image.alpha_composite(img1.convert('RGBA'), img2.convert('RGBA'))
composite.save('composite.png')
Batch Processing
Example
import os
from PIL import Image
input_dir = 'input_images/'
output_dir = 'output_images/'
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for filename in os.listdir(input_dir):
if filename.endswith('.jpg'):
img = Image.open(os.path.join(input_dir, filename))
# Convert to grayscale and save
gray_img = img.convert('L')
gray_img.save(os.path.join(output_dir, f"gray_{filename}"))
from PIL import Image
input_dir = 'input_images/'
output_dir = 'output_images/'
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for filename in os.listdir(input_dir):
if filename.endswith('.jpg'):
img = Image.open(os.path.join(input_dir, filename))
# Convert to grayscale and save
gray_img = img.convert('L')
gray_img.save(os.path.join(output_dir, f"gray_{filename}"))