1. Introduction to Pillow Library
- Function: Image opening/save, size adjustment, filter application, color processing, drawing, text addition, etc.
- Supported formats: JPEG, PNG, GIF, BMP, TIFF and other 30+ formats
- Core modules: Image, ImageDraw, ImageFilter, ImageEnhance, etc.
2. Install Pillow
pip install Pillow
3. Core functions and code examples
1. Open/Save the image
from PIL import Image # Open the imageimg = ("") print(f"Format: {}, size: {}, model: {}") # Save the image (automatically recognize the format)("") # Convert format("", "WEBP")
2. Basic image operation
# Adjust sizeresized = ((300, 200)) # Rotate (expand parameter prevents cropping)rotated = (45, expand=True) # Crop (left, upper, right, lower)cropped = ((100, 100, 400, 400)) # Thumbnail generation (maintaining proportion)((128, 128)) ("")
3. Image processing
from PIL import ImageFilter, ImageEnhance # Apply filtersblurred = ((radius=2)) contour = () # Color adjustmentgray = ("L") # Turn to grayscaleenhancer = (img) contrast_img = (2.0) # 2x contrast enhancement # Adjust brightnessbright_enhancer = (img) bright_img = bright_enhancer.enhance(1.5)
4. Draw graphics and text
from PIL import ImageDraw, ImageFont # Create a new canvascanvas = ("RGB", (500, 500), color=(255, 255, 255)) draw = (canvas) # Draw shapes([50, 50, 200, 200], fill="red", outline="blue") ([250, 250, 400, 400], fill="yellow") # Add texttry: font = ("", 24) except IOError: font = ImageFont.load_default() ((100, 300), "Hello Pillow!", fill="black", font=font) ()
5. Advanced Operation
# Image synthesisoverlay = ("RGBA", , (255, 0, 0, 100)) composite = Image.alpha_composite(("RGBA"), overlay) # Pixel level operationpixels = () for y in range(): for x in range(): r, g, b = pixels[x, y] pixels[x, y] = (r//2, g//2, b//2) # Darkening effect # Create a GIFimages = [img, rotated, cropped] images[0].save("", save_all=True, append_images=images[1:], duration=500, loop=0)
4. Comprehensive application example: Add watermark
def add_watermark(input_path, output_path, text): base = (input_path).convert("RGBA") txt = ("RGBA", , (255,255,255,0)) draw = (txt) font = ("", 36) # Calculate text position text_width, text_height = (text, font) x = - text_width - 10 y = - text_height - 10 ((x, y), text, font=font, fill=(255,255,255,128)) combined = Image.alpha_composite(base, txt) ("RGB").save(output_path) add_watermark("", "", "© Your Brand")
5. Things to note
- Use when handling transparent channels
RGBA
model - JPEG format does not support transparency, and the conversion mode is required when saving
- Memory consumption is required for processing large-size images
- use
with
Statement management file resources:
with ("large_image.tiff") as img: # Process images
VI. Typical application scenarios
- Generate thumbnails in batches
- Add copyright information to the image
- Image format conversion tool
- Create dynamic verification code pictures
- Image Data Augmentation (Machine Learning)
7. Advanced image processing skills
1. Channel operation and mixing
from PIL import Image, ImageChops # Separate RGB channelsr, g, b = () # Adjust the channel order when merging (for example, create pseudo-infrared effects)modified = ("RGB", (b, g, r)) # Channel mixing (mathematical operations)diff = (img1, img2) # Pixel Difference Graphblended = (img1, img2, alpha=0.3) # Image overlay
2. Color space conversion
# CMYK to RGBcmyk_img = ("cmyk_image.tiff").convert("RGB") # Convert to HSV color space (requires supplemented by third-party libraries)# Note: Pillow does not natively support HSV operations, but can be implemented through numpyimport numpy as np hsv_array = (("HSV")) # ...processing HSV arrays...modified_img = (hsv_array, "HSV").convert("RGB")
3. EXIF metadata processing
# Read EXIF dataexif_data = img._getexif() if exif_data: from import TAGS for tag_id, value in exif_data.items(): tag_name = (tag_id, tag_id) print(f"{tag_name}: {value}") # Rotate the image (automatically corrected according to EXIF direction information)from PIL import ImageOps fixed_img = ImageOps.exif_transpose(img)
8. Performance optimization skills
1. Large picture processing plan
# Process large images in blockstile_size = 512 for y in range(0, , tile_size): for x in range(0, , tile_size): box = (x, y, x+tile_size, y+tile_size) tile = (box) # Handle chunking... (tile, box)
2. Use memory optimization mode
# Use LA mode to replace RGBA (save 25% memory)gray_alpha = ("LA")
3. Accelerate pixel operation
# Use numpy to speed up pixel processingimport numpy as np arr = (img) arr = arr // 2 # Rapid darkening treatmentfast_processed = (arr)
9. Practical project examples
1. Generate verification code pictures
from PIL import Image, ImageDraw, ImageFont, ImageFilter import random def generate_captcha(text, size=(200, 80)): img = ("RGB", size, (255,255,255)) draw = (img) # Draw the interference line for _ in range(8): start = ((0, size[0]), (0, size[1])) end = ((0, size[0]), (0, size[1])) ([start, end], fill=((0,255), (0,255), (0,255)), width=2) # Add text font = ("", 36) text_width, text_height = ((0,0), text, font=font)[2:] x = (size[0] - text_width) / 2 y = (size[1] - text_height) / 2 # Text distortion for i, char in enumerate(text): ( (x + i*30 + (-5,5), y + (-5,5)), char, font=font, fill=((0,160), (0,160), (0,160)) ) # Add filters img = (ImageFilter.SMOOTH_MORE) return img captcha = generate_captcha("3A4B") ("")
2. Batch picture processor
import os from PIL import Image, ImageOps def batch_process(input_dir, output_dir, process_func): (output_dir, exist_ok=True) for filename in (input_dir): if ().split('.')[-1] not in ['jpg', 'png', 'jpeg']: continue with ((input_dir, filename)) as img: processed = process_func(img) ((output_dir, filename)) # Example processing function: Convert to sketch styledef sketch_effect(img): gray = ("L") invert = (gray) blur = ((3)) return (gray, blur) batch_process("./photos", "./processed", sketch_effect)
10. Image analysis function
1. Histogram analysis
histogram = () # Get the R channel histogram (first 256 values)red_hist = histogram[:256] # Draw a simple histogram (requires matplotlib)import as plt (figsize=(10,4)) ("Red Channel Histogram") (range(256), red_hist, color='red') ()
2. Edge detection
from PIL import ImageFilter edges = ("L").filter(ImageFilter.FIND_EDGES) edges = (lambda x: 255 if x > 50 else 0) # Binary()
11. Error handling and debugging
1. Robust image processing
try: with ("") as img: # Processing images...except IOError as e: print(f"Unable to open image file: {str(e)}") except : print("The image size is too large, please check if it is safe!")
2. Memory management skills
# Force release image memory() # Explicitly close # Use with statements to automatically managewith ("large_image.tiff") as img: thumbnail = () ((1024, 1024)) ("") # Note: The original image is closed at this time
12. Integrate with other libraries
1. Use in combination with OpenCV
import cv2 import numpy as np from PIL import Image # Pillow -> OpenCV pil_img = ("") cv_img = (pil_img.convert('RGB'))[:, :, ::-1] # RGB to BGR # OpenCV -> Pillow cv_img = (cv_img, cv2.COLOR_BGR2RGB) pil_img = (cv_img)
2. Combined with Matplotlib display
import as plt (img) ('off') ("Pillow Image Display") ()
13. Expand knowledge
1. Custom filters
from PIL import ImageFilter class CustomFilter(): name = "CustomSharpen" filterargs = (3, 3), 16, 0, ( -1, -1, -1, -1, 24, -1, -1, -1, -1 ) sharpened = (CustomFilter())
2. Plugin image format support
# After installing the additional decoder, it can support WebP animations, etc.# Need to install: pip install pillow-heifimport pillow_heif heif_file = pillow_heif.open_heif("") heif_img = ( heif_file.mode, heif_file.size, heif_file.data, "raw", heif_file.mode, heif_file.stride, )
14. Best Practice Suggestions
-
Format selection policy
- Web page usage:
JPEG
(photo),PNG
(Transparent/Icon),WEBP
(Modern format) - Printing using:
TIFF
(No loss),PDF
(Vector) - Dynamic pictures:
GIF
(Simple animation),APNG
(High-quality animation)
- Web page usage:
Quality parameter optimization
("", quality=85, # Mass Value (1-100) optimize=True, # Enable optimization progressive=True) # Progressive JPEG
- Multi-frame image processing(GIF/TIFF)
with ("") as img: for frame in (img): # Process every frame (f"frame_{()}.png")
15. Advanced image analysis technology
1. Histogram matching (tonal mapping)
import numpy as np from PIL import Image def histogram_matching(source, template): """Match the histogram of the source image to the template image""" src = (('L')).flatten() tgt = (('L')).flatten() # Calculate the cumulative distribution function src_vals, src_counts = (src, return_counts=True) tgt_vals, tgt_counts = (tgt, return_counts=True) src_cdf = (src_counts) / tgt_cdf = (tgt_counts) / # Create a mapping table lut = (src_cdf, tgt_cdf, tgt_vals) return (lut) source_img = ("") template_img = ("") matched = histogram_matching(source_img, template_img)
2. Image segmentation (threshold processing)
from PIL import ImageOps # Otsu automatic threshold algorithmgray = ('L') thresh = (lambda x: 255 if x > 130 else 0) # Manual threshold # Automatic threshold (scikit-image required)from import threshold_otsu arr = (gray) thresh_val = threshold_otsu(arr) auto_thresh = ((arr > thresh_val).astype(np.uint8) * 255)
16. Professional field application
1. Medical imaging processing (DICOM)
# pydicom needs to be installedimport pydicom from PIL import Image ds = ("") arr = ds.pixel_array.astype(float) arr = (arr - ()) / (() - ()) * 255 medical_img = ((np.uint8)) medical_img.save("mri_preview.png")
2. Satellite image processing (multi-spectral analysis)
# Load multi-band imageswith ("") as img: bands = [(band) for band in ('R', 'G', 'B', 'NIR')] # Calculate NDVI (Normalized Vegetation Index)red = (bands[0], dtype=np.float32) nir = (bands[3], dtype=np.float32) ndvi = (nir - red) / (nir + red + 1e-10) # Prevent division by zero # Visualize NDVIndvi_img = (np.uint8((ndvi + 1) * 127.5)) ndvi_img.save("ndvi_map.png")
17. Three-dimensional image processing
1. Z-Stack image synthesis
import glob from PIL import Image, ImageMath # Load the focus stack imagez_images = [(f) for f in sorted(("z_stack_*.tif"))] # Create a fully focused imagedef focus_metric(pixels): return (pixels) # Use standard deviation as a clarity indicator depth_map = (z_images[0].size[::-1]) final_img = np.zeros_like((z_images[0])) for i in range(z_images[0].height): for j in range(z_images[0].width): pixels = [(img)[i,j] for img in z_images] sharpness = [focus_metric(p) for p in pixels] best_idx = (sharpness) final_img[i,j] = pixels[best_idx] depth_map[i,j] = best_idx (final_img).save("") (np.uint8(depth_map * 255 / len(z_images))).save("depth_map.png")
18. GPU acceleration processing
1. Use CUDA to accelerate (cupy needs to be installed)
import cupy as cp from PIL import Image def gpu_filter(img): # Convert to GPU img_gpu = (img) # Custom kernel functions (edge detection) kernel = ([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]], dtype=cp.float32) # Convolution operation filtered = cp.zeros_like(img_gpu) for c in range(3): filtered[:, :, c] = cp.convolve2d(img_gpu[:, :, c], kernel, mode='same') # Return to CPU return ((filtered).astype(np.uint8)) original = ("gpu_demo.jpg") accelerated = gpu_filter(original)
19. Integration with deep learning frameworks
1. PyTorch Data Enhancement Pipeline
from torchvision import transforms from PIL import Image class AdvancedAugmentation: def __init__(self): = ([ ([ lambda x: .adjust_sharpness(x, 2), lambda x: (x).enhance(0.8) ], p=0.5), (distortion_scale=0.3), (brightness=0.2, contrast=0.2) ]) def __call__(self, img): return (img) augmenter = AdvancedAugmentation() img = ("training_sample.jpg") augmented = augmenter(img)
20. Plug-in development and extension
1. Custom image format decoder
from PIL import Image, ImageFile class RAWDecoder(): def decode(self, buffer): # Implement custom RAW format parsing raw_data = self._parse_custom_format(buffer) self.set_as_raw(bytes(raw_data)) return -1, 0 # Register the decoderImage.register_decoder("MYRAW", RAWDecoder) Image.register_extensions("MYRAW", [".raw"])
2. Write filter plug-ins
from PIL import ImageFilter class KaleidoscopeFilter(): name = "Kaleidoscope" filterargs = (5, 5), 16, 0, ( 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1 ) (KaleidoscopeFilter()).show()
21. Engineering Practice
1. Image processing microservice architecture
# Build a REST API using FastAPIfrom fastapi import FastAPI, File, UploadFile from PIL import Image import io app = FastAPI() @("/enhance") async def enhance_image(file: UploadFile = File(...)): img = ((await ())) # Execute processing pipeline enhanced = (('RGB') .filter() .enhance(contrast=1.2) .resize((1024, 768))) byte_arr = () (byte_arr, format='JPEG') return Response(content=byte_arr.getvalue(), media_type="image/jpeg")
2. Automated testing framework
import unittest from PIL import Image, ImageChops class ImageTests(): def test_watermark(self): original = ('RGB', (800, 600), 'white') watermarked = add_watermark((), "Test") # Verify the location and content of the watermark diff = (original, watermarked) (()[2], 700) # Check the changes in the lower right corner def test_performance(self): large_img = ('RGB', (10000, 10000)) start = () large_img.thumbnail((1000, 1000)) (() - start, 1.0)
22. Frontier application cases
1. AI Art Style Transfer
# Combining Pillow with deep learning modelsdef style_transfer(content_img, style_img): # Load pretrained models (torch and torchvision need to be installed) from import vgg19 model = vgg19(pretrained=True).() # Feature extraction and style transfer algorithm... # (Implement specific migration logic here) return (output) content = ("") style = ("starry_night.jpg") artwork = style_transfer(content, style)
2. Dynamic QR code generation
from PIL import Image, ImageDraw import qrcode def animated_qr(data, logo_path): base = (data).convert('RGBA') logo = (logo_path).resize((100, 100)) frames = [] for angle in range(0, 360, 30): rotated_logo = (angle) frame = () frame.alpha_composite(rotated_logo, ((-100)//2, (-100)//2)) (frame) frames[0].save("qr_animation.gif", save_all=True, append_images=frames[1:], duration=200, loop=0, optimize=True)
23. Debugging and performance analysis
1. Memory analysis tool
import tracemalloc from PIL import Image () # Test code blockwith ("") as img: processed = ((2000, 2000)) ("") snapshot = tracemalloc.take_snapshot() top_stats = ('lineno') print("[Top 10 Memory Consumption]") for stat in top_stats[:10]: print(stat)
2. Performance analysis
# Use cProfile to analyze processing flowpython -m cProfile -s cumulative image_processor.py
24. Things to note when developing cross-platform
- Font processing compatibility
# Safe Font Loading Solutiondef safe_font_loading(font_path, fallback_size=20): try: return (font_path, size=fallback_size) except IOError: print(f"Font {font_path} Loading failed,使用系统默认Font") return ImageFont.load_default()
- Path processing specifications
from pathlib import Path input_path = Path("input_images") / "special_chars_test.jpg" output_path = input_path.with_name(f"processed_{input_path.name}")
25. Resources and Advanced Learning
-
Extended reading of official documents
- Advanced image transformation:
()
method - Block encoder:
JpegEncoder
andWebPEncoder
- Image protocol: Support
HTTP
、FTP
With the protocol directly read
- Advanced image transformation:
By mastering these advanced technologies, you will be able to:
- Processing the image analysis requirements in the field of expertise
- Build an enterprise-level image processing system
- Optimize processing flow to achieve industrial-grade performance
- Develop custom image processing extensions
- Integrate Pillow to modern AI workflows
Suggested practice directions: develop intelligent photo album management systems, create medical image analysis tools, build cloud image processing API services, or implement real-time video frame processing pipelines. Keep following Pillow's GitHub repository (/python-pillow/Pillow) for the latest feature updates.
The above is the detailed introduction of the Python Pillow library and the code examples. For more information about the introduction of the Python Pillow library, please pay attention to my other related articles!