Real-ESRGAN

0.3.0 · active · verified Fri Apr 17

Real-ESRGAN provides state-of-the-art practical algorithms for general image restoration, particularly focusing on super-resolution. It is currently at version 0.3.0 and sees active development with several minor and major releases annually, bringing new models and features.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `RealESRGANer` to upscale a dummy image programmatically. It uses the `RealESRGAN_x4plus` model, which will be automatically downloaded on first use. By default, it runs on CPU, but can be switched to 'cuda' for GPU acceleration if PyTorch with CUDA is installed.

import os
from PIL import Image
import numpy as np

# Assuming realesrgan is installed via pip
try:
    from realesrgan import RealESRGANer
except ImportError:
    print("RealESRGAN is not installed. Please run 'pip install realesrgan'")
    exit(1)

# Create a dummy image for demonstration
input_image_path = "temp_input_64x64.png"
output_image_path = "temp_output_upscaled.png"
dummy_image = Image.new('RGB', (64, 64), color = 'red')
dummy_image.save(input_image_path)

try:
    # Initialize RealESRGANer with a common model (auto-downloads if not present)
    # Using 'cpu' for device ensures it runs without a CUDA setup.
    # For GPU, change 'cpu' to 'cuda' and ensure PyTorch with CUDA is installed.
    upscaler = RealESRGANer(
        model_name='RealESRGAN_x4plus', # A widely used general-purpose model
        netscale=4, # The upsampling factor the model was trained for
        outscale=4, # Desired output upsampling factor (should match netscale for best results)
        tile=0, # Set to 0 to process the entire image at once. For large images/limited VRAM, use e.g. 600.
        tile_pad=10,
        pre_pad=0,
        device='cpu' # Use 'cuda' if a compatible GPU and PyTorch+CUDA are available
    )

    # Load and convert image
    img = Image.open(input_image_path).convert('RGB')
    img_np = np.array(img) # Convert PIL Image to NumPy array for processing

    # Perform enhancement
    upscaled_image_np, _ = upscaler.enhance(img_np, outscale=4)

    # Convert back to PIL Image and save
    upscaled_image_pil = Image.fromarray(upscaled_image_np)
    upscaled_image_pil.save(output_image_path)

    print(f"Image upscaled successfully and saved to {output_image_path}")

except Exception as e:
    print(f"An error occurred during upscaling: {e}")
    print("Common issues: missing models (check network), CUDA not available/misconfigured, out of memory.")
    if "cuda" in str(e).lower() and "memory" in str(e).lower():
        print("Consider reducing `tile` size in RealESRGANer or processing smaller images.")
    if "model_name" in str(e) or "model_path" in str(e):
        print("Ensure the specified `model_name` is valid or `model_path` points to an existing model file.")
finally:
    # Clean up dummy files
    if os.path.exists(input_image_path):
        os.remove(input_image_path)
    if os.path.exists(output_image_path):
        os.remove(output_image_path)

view raw JSON →