A Python implementation for upscaling low-resolution images using Real-ESRGAN (Real-Enhanced Super-Resolution Generative Adversarial Networks). This project provides an easy-to-use interface for enhancing image quality with 2x, 4x, or 8x upscaling.
|
π Multiple Scale Options
β‘ GPU Acceleration
π¦ Batch Processing
|
ποΈ Visual Comparison
πΌοΈ Format Support
π οΈ Easy Integration
|
pip install git+https://github.com/sberbank-ai/Real-ESRGAN.git
pip install huggingface_hub==0.9.0numpy
opencv-python
Pillow
torch>=1.7
torchvision>=0.8.0
tqdm
huggingface-hub
matplotlib (for visualization)
from RealESRGAN import RealESRGAN
from PIL import Image
import numpy as np
import torch
# Initialize device
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print('device:', device)
# Create model with desired scale (2, 4, or 8)
model_scale = 4
model = RealESRGAN(device, scale=model_scale)
model.load_weights(f'weights/RealESRGAN_x{model_scale}.pth')# Load and process image
lr_image = Image.open('path/to/low_res_image.jpg').convert('RGB')
lr_np = np.array(lr_image)
# Generate super-resolved image
sr_output = model.predict(lr_np)
# Convert to PIL Image and save
if isinstance(sr_output, Image.Image):
sr_image = sr_output
else:
sr_image = Image.fromarray(np.clip(sr_output, 0, 255).astype(np.uint8))
sr_image.save('path/to/output_image.jpg')import os
import matplotlib.pyplot as plt
# Set up folders
lr_folder = 'path/to/low_resolution_images'
hr_folder = 'path/to/high_resolution_ground_truth' # Optional for comparison
sr_folder = 'results'
os.makedirs(sr_folder, exist_ok=True)
# Get image list
image_names = sorted([f for f in os.listdir(lr_folder)
if f.lower().endswith(('.png', '.jpg', '.jpeg'))])
# Process all images
for img_name in image_names:
print(f"Processing: {img_name}")
lr_path = os.path.join(lr_folder, img_name)
try:
# Load and process
lr_image = Image.open(lr_path).convert('RGB')
lr_np = np.array(lr_image)
sr_output = model.predict(lr_np)
# Save result
if isinstance(sr_output, Image.Image):
sr_image_pil = sr_output
else:
sr_image_pil = Image.fromarray(np.clip(sr_output, 0, 255).astype(np.uint8))
sr_image_pil.save(os.path.join(sr_folder, img_name))
except Exception as e:
print(f"Error processing {img_name}: {e}")def show_side_by_side(lr, sr, hr, title):
fig, ax = plt.subplots(1, 3, figsize=(15, 5))
ax[0].imshow(lr)
ax[0].set_title('Low-Res')
ax[0].axis('off')
ax[1].imshow(sr)
ax[1].set_title('Super-Resolved (4x)')
ax[1].axis('off')
ax[2].imshow(hr)
ax[2].set_title('High-Res (Ground Truth)')
ax[2].axis('off')
plt.suptitle(title)
plt.tight_layout()
plt.show()
# Use in processing loop
show_side_by_side(lr_image, sr_image_pil, hr_image, img_name)| Scale | Description | Use Case |
|---|---|---|
| 2x | Doubles the resolution | Quick enhancement, real-time processing |
| 4x | Quadruples the resolution | Balanced quality and performance |
| 8x | 8x resolution increase | Maximum quality, resource intensive |
| Component | Requirement | Notes |
| GPU | CUDA-compatible recommended | Significantly faster processing |
| CPU | Any modern processor | Fallback option if no GPU |
| Memory | 4GB+ RAM/VRAM | Depends on image size and batch size |
project/
βββ weights/
β βββ RealESRGAN_x2.pth
β βββ RealESRGAN_x4.pth
β βββ RealESRGAN_x8.pth
βββ input_images/
βββ results/
βββ your_script.py
π‘ Pro Tips
- Model weights are automatically downloaded on first use
- Processing time varies based on image size and hardware
- GPU acceleration provides 5-10x speed improvement
- Larger scale factors (8x) require more computational resources
π¨ CUDA Out of Memory
Solution:
- Reduce batch size
- Process smaller images
- Use lower scale factor (2x instead of 8x)
π¦ Dependency Conflicts
Solution:
- Use the specified
huggingface_hub==0.9.0version - Create a fresh virtual environment
- Install dependencies in the exact order provided
β οΈ Model Loading Errors
Solution:
- Ensure stable internet connection for weight download
- Check available disk space
- Verify CUDA installation if using GPU
The implementation includes robust error handling for individual image processing failures, allowing batch operations to continue even if some images fail to process.
Real-ESRGAN excels at enhancing:
| πΌοΈ Natural Images Photographs, landscapes |
π¨ Artwork Illustrations, drawings |
π» Digital Content Screenshots, UI elements |
π Fine Details Textures, patterns |
β¨ Quality Promise: The model preserves important visual details while reducing artifacts commonly found in traditional upscaling methods.