AI Photo Restoration
AI restoration removes damage from old photographs: scratches, stains, scuffs, pixelation, compression artifacts, blur, noise. Combines several specialized models in a pipeline.
Full Restoration Pipeline
from PIL import Image
import cv2
import numpy as np
import io
class PhotoRestorationPipeline:
def restore(self, damaged_photo: bytes) -> bytes:
# 1. Remove scratches and stains (GFPGAN + inpainting)
# 2. Upscaling (Real-ESRGAN)
# 3. Face restoration (GFPGAN)
# 4. Denoising
image = Image.open(io.BytesIO(damaged_photo)).convert("RGB")
img_np = np.array(image)
img_np = self.remove_scratches(img_np)
img_np = self.upscale(img_np)
img_np = self.restore_faces(img_np)
result = Image.fromarray(img_np)
buf = io.BytesIO()
result.save(buf, format="PNG")
return buf.getvalue()
Real-ESRGAN Upscaling
from basicsr.archs.rrdbnet_arch import RRDBNet
from realesrgan import RealESRGANer
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
upsampler = RealESRGANer(
scale=4,
model_path="RealESRGAN_x4plus.pth",
model=model,
tile=512, # tiling for large images
tile_pad=10,
pre_pad=0,
half=True # fp16
)
def upscale_image(img_np: np.ndarray, scale: int = 4) -> np.ndarray:
output, _ = upsampler.enhance(img_np, outscale=scale)
return output
GFPGAN Face Restoration
from gfpgan import GFPGANer
gfpgan = GFPGANer(
model_path="GFPGANv1.4.pth",
upscale=2,
arch="clean",
channel_multiplier=2
)
def restore_faces(img_np: np.ndarray) -> np.ndarray:
_, _, restored_img = gfpgan.enhance(
img_np,
has_aligned=False,
only_center_face=False,
paste_back=True,
weight=0.5 # 0 = GFPGAN, 1 = original (balance)
)
return restored_img
REST API
from fastapi import FastAPI, UploadFile, File
app = FastAPI()
pipeline = PhotoRestorationPipeline()
@app.post("/restore")
async def restore_photo(file: UploadFile = File(...)):
original_bytes = await file.read()
restored_bytes = pipeline.restore(original_bytes)
return Response(content=restored_bytes, media_type="image/png")
Real-ESRGAN provides 4x upscaling with detail restoration. GFPGAN specializes in faces — critical for family photo archives. Both combined cover 90% of restoration tasks. Timeline: pipeline deployment — 1–2 days. Service with web upload interface and before/after comparison — 1 week.







