Phase 2: Image Generation · 55 min · Python · Hugging Face Diffusers · Real-ESRGAN
Inpainting, Outpainting, Upscaling & Restoration
Generation is step one — post-processing (inpainting, upscaling, face restoration) is what makes images production-ready.
Hiring signal: Post-processing pipeline knowledge (inpainting, upscaling, face restoration) demonstrates you understand the full image generation pipeline, not just the generation step.
What you will learn
- Implement inpainting: selective regeneration of specific image regions
- Implement outpainting: extending image boundaries beyond original dimensions
- Apply upscaling with ESRGAN and Real-ESRGAN for higher resolution
- Use face restoration (CodeFormer, GFPGAN) to fix faces in generated images
The Problem
A team generates images with FLUX.1 Schnell at 1024x1024. The images look good, but:
- A product image has a distorted hand — needs inpainting to fix
- A hero image needs to be 4K for print — needs upscaling
- A portrait has blurry faces — needs face restoration
- A landscape needs to be wider — needs outpainting
Generation is only step one. Post-processing is what makes images production-ready. A pipeline without post-processing produces raw outputs that aren't suitable for production use.
What you'll build
A post-processing pipeline that takes a generated image and applies: (1) face restoration with CodeFormer, (2) 4x upscaling with Real-ESRGAN, (3) inpainting to fix a specific region. Measure processing time for each step.
Inpainting
Inpainting selectively regenerates specific regions of an image while keeping the rest unchanged.
from diffusers import StableDiffusionXLInpaintPipeline
from PIL import Image
# Load inpainting model
pipe = StableDiffusionXLInpaintPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
torch_dtype=torch.float16,
)
# Load image and mask
image = Image.open("generated_image.png").resize((1024, 1024))
mask = Image.open("mask.png").resize((1024, 1024)) # White = regenerate, black = keep
# Inpaint the masked region
result = pipe(
prompt="a hand holding a coffee cup, detailed",
image=image,
mask_image=mask,
num_inference_steps=30,
guidance_scale=7.5,
strength=0.8, # How much to change the masked region
).images[0]
Inpainting Parameters
| Parameter | Typical | Effect |
|---|
strength | 0.6-0.9 | How much to change the masked region (1.0 = full regeneration) |
guidance_scale | 7.5 | Prompt adherence in the masked region |
num_inference_steps | 25-30 | Quality of inpainted region |
mask_blur | 4-8 pixels | Blends inpainted region with surrounding pixels |
Creating Masks
import numpy as np
from PIL import Image, ImageDraw
# Create a mask (white = regenerate, black = keep)
mask = Image.new("L", (1024, 1024), 0) # Black background
draw = ImageDraw.Draw(mask)
# Draw white rectangle over the region to fix
draw.rectangle([300, 400, 500, 600], fill=255) # Fix this region
mask.save("mask.png")
Unlock the full lesson
You've read the first 2 sections. The rest of this lesson covers Outpainting, Upscaling with Real-ESRGAN, Face Restoration with CodeFormer, Production Post-Processing Pipeline, Pipeline Order Matters, Processing Time Expectations, Key Takeaways, What's Next — plus a hands-on lab, quiz, and project artifact.
Create a free account to unlock Phase 0 and Phase 1 of every course — no credit card.
Browse all courses · View pricing · DeVenture Academy