Skip to content

Gtajisan/NAFNet-Image-Restoration-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

52 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

NAFNet Image Restoration API

Python 3.8+ License: MIT Code style: PEP8 Flask 3.1.2 Production Ready Version 1.0.0

Enterprise-grade Image Restoration & Enhancement API

Restore, denoise, and enhance images with production-ready Flask API and modern web interface.

Quick Start β€’ Installation β€’ API Docs β€’ Examples β€’ Deployment β€’ Contributing


πŸ“‹ Table of Contents


Overview

NAFNet Image Restoration API is a professional-grade, production-ready REST API for image restoration and enhancement. Built with Flask and optimized for high performance, it combines advanced image processing techniques with a modern, intuitive web interface.

Use Cases

  • Photo Restoration: Restore old or degraded photographs
  • Noise Reduction: Remove noise from low-light or high-ISO images
  • Quality Enhancement: Improve overall image clarity and sharpness
  • Batch Processing: Process multiple images efficiently
  • Integration: Embed in larger applications via REST API
  • Enterprise Deployment: Run on Docker with health checks and monitoring

✨ Features

🎨 Web Interface

  • Drag-and-drop upload with validation
  • Real-time preview showing before/after comparison
  • Automatic download of processed images
  • Responsive design for desktop, tablet, and mobile
  • Modern UI with smooth animations and feedback

πŸ’» RESTful API

  • Single endpoint for restoration: POST /api/fix
  • Info endpoint for API details: GET /api/info
  • JSON error responses with descriptive messages
  • Content negotiation and proper HTTP status codes
  • File size limits (50MB) for security

πŸ”§ Image Processing Pipeline

  1. Denoise - Median filtering to remove noise while preserving edges
  2. Smooth - Gaussian blur for artifact reduction
  3. Enhance Contrast - 1.3x multiplier for visual impact
  4. Sharpen - 1.2x multiplier for edge definition
  5. Saturate Colors - 1.1x multiplier for color vibrancy

πŸš€ Production Ready

  • Error handling with validation on all inputs
  • Security - File size limits, input validation, safe file operations
  • Performance - Optimized image processing, efficient memory usage
  • Monitoring - Docker health checks, structured logging
  • Documentation - Comprehensive API docs and examples

πŸ“¦ Enterprise Support

  • Docker multi-stage build with optimization
  • CI/CD GitHub Actions workflows for automated testing and linting
  • Versioning semantic versioning (SemVer)
  • Packaging proper Python package metadata (pyproject.toml)
  • Code Quality PEP 8 compliance tools configured

πŸš€ Quick Start

Prerequisites

  • Python 3.8 or higher
  • pip package manager

Local Setup (30 seconds)

# 1. Clone repository
git clone https://github.com/Gtajisan/NAFNet-Image-Restoration-API.git
cd NAFNet-Image-Restoration-API

# 2. Install dependencies
pip install -r requirements.txt

# 3. Run application
python app.py

# 4. Open browser
# Navigate to http://localhost:5000

Docker Setup

# Build image
docker build -t nafnet-api:latest .

# Run container
docker run -p 5000:5000 nafnet-api:latest

# Access at http://localhost:5000

Installation

System Requirements

Component Minimum Recommended
Python 3.8 3.11
RAM 512MB 2GB
Disk 500MB 2GB
CPU 1 Core 2+ Cores

Step-by-Step Installation

1. Clone Repository

git clone https://github.com/Gtajisan/NAFNet-Image-Restoration-API.git
cd NAFNet-Image-Restoration-API

2. Create Virtual Environment

# Linux/macOS
python -m venv venv
source venv/bin/activate

# Windows
python -m venv venv
venv\Scripts\activate

3. Install Dependencies

pip install -r requirements.txt

Dependency Details:

  • Flask 3.1.2 - Web framework
  • Pillow 12.0.0 - Image processing
  • NumPy 2.3.5 - Numerical computations
  • SciPy 1.16.3 - Scientific computing
  • Werkzeug 3.1.4 - WSGI utilities

4. Run Application

python app.py

Expected Output:

==================================================
πŸš€ NAFNet Image Restoration API
==================================================
πŸ“ Running on http://0.0.0.0:5000
🌐 Open in browser: http://localhost:5000
==================================================

5. Verify Installation

# In another terminal
curl http://localhost:5000/api/info

Expected Response:

{
  "name": "NAFNet Image Restoration API",
  "version": "1.0",
  "endpoints": {
    "GET /": "Web interface",
    "GET /api/info": "Get API info",
    "POST /api/fix": "Upload image for restoration"
  }
}

πŸ’» API Documentation

Base URL

http://localhost:5000

Authentication

Currently no authentication required. For production, implement OAuth 2.0 or API keys.

Endpoints

POST /api/fix

Restore and enhance a single image.

Request:

curl -X POST \
  -F "file=@image.jpg" \
  http://localhost:5000/api/fix \
  -o restored.png

Parameters:

Parameter Type Required Description
file File Yes Image file (JPG, PNG, WebP, GIF)

Response:

  • Status: 200 OK
  • Content-Type: image/png
  • Body: PNG image file (binary)

Error Responses:

Code Message Reason
400 "Upload an image as 'file'" Missing file parameter
400 "No file selected" Empty filename
500 "Error message" Processing error

GET /api/info

Retrieve API information and available endpoints.

Request:

curl http://localhost:5000/api/info

Response: 200 OK

{
  "name": "NAFNet Image Restoration API",
  "version": "1.0",
  "endpoints": {
    "GET /": "Web interface",
    "GET /api/info": "Get API info",
    "POST /api/fix": "Upload image for restoration"
  }
}

GET /

Access web interface for interactive image restoration.

Response: 200 OK - HTML page with drag-and-drop upload


🎯 Usage Examples

cURL

# Basic restoration
curl -X POST \
  -F "file=@photo.jpg" \
  http://localhost:5000/api/fix \
  -o restored.png

# With verbose output
curl -v -X POST \
  -F "file=@photo.jpg" \
  http://localhost:5000/api/fix \
  -o restored.png

Python

import requests

# Single image
with open('image.jpg', 'rb') as f:
    response = requests.post(
        'http://localhost:5000/api/fix',
        files={'file': f}
    )

if response.status_code == 200:
    with open('restored.png', 'wb') as out:
        out.write(response.content)
    print('βœ“ Image restored successfully')
else:
    print(f'βœ— Error: {response.status_code}')

JavaScript / Fetch

const fileInput = document.getElementById('fileInput');
const file = fileInput.files[0];

const formData = new FormData();
formData.append('file', file);

fetch('/api/fix', {
  method: 'POST',
  body: formData
})
.then(response => response.blob())
.then(blob => {
  const url = URL.createObjectURL(blob);
  const a = document.createElement('a');
  a.href = url;
  a.download = 'restored.png';
  a.click();
})
.catch(error => console.error('Error:', error));

Using Provided Client

from api_examples import NAFNetClient

client = NAFNetClient()

# Get API info
info = client.get_info()

# Restore single image
client.restore_image('input.jpg', 'output.png')

# Batch process
client.batch_restore('./images', './restored')

🌐 Deployment

Replit (Recommended for Development)

python app.py

Gunicorn (Production)

pip install gunicorn
gunicorn -w 4 -b 0.0.0.0:5000 --timeout 60 app:app

Docker (Production)

# Build image
docker build -t nafnet-api:latest .

# Run with resource limits
docker run \
  -p 5000:5000 \
  --memory=1g \
  --cpus=1 \
  nafnet-api:latest

# With health checks
docker run \
  -p 5000:5000 \
  --health-cmd='curl localhost:5000/api/info' \
  --health-interval=30s \
  nafnet-api:latest

Docker Compose

version: '3.8'
services:
  api:
    build: .
    ports:
      - "5000:5000"
    environment:
      - FLASK_ENV=production
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:5000/api/info"]
      interval: 30s
      timeout: 10s
      retries: 3

Cloud Platforms

AWS EC2

sudo apt-get update
sudo apt-get install python3-pip python3-venv
git clone https://github.com/Gtajisan/NAFNet-Image-Restoration-API.git
cd NAFNet-Image-Restoration-API
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
gunicorn -w 4 -b 0.0.0.0:5000 app:app

Heroku

# Create Procfile
echo "web: gunicorn app:app" > Procfile

# Deploy
git push heroku main

Replit

Simply run the project - Replit handles deployment automatically.


πŸ—οΈ Architecture

System Design

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         Web Interface (HTML/JS)          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                    ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚        Flask Application (app.py)        β”‚
β”‚  β€’ Request validation                    β”‚
β”‚  β€’ Route handling                        β”‚
β”‚  β€’ Error handling                        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                    ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    Image Processing Pipeline            β”‚
β”‚  β€’ Image loading (PIL)                  β”‚
β”‚  β€’ Denoising (SciPy)                    β”‚
β”‚  β€’ Enhancement (PIL)                    β”‚
β”‚  β€’ Saving (PIL)                         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                    ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         Output (PNG Image)              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Image Processing Pipeline

  1. Load - Read image with PIL, convert to RGB
  2. Denoise - Apply median filter via SciPy
  3. Smooth - Apply Gaussian blur
  4. Enhance - Adjust contrast (1.3x), sharpness (1.2x), saturation (1.1x)
  5. Save - Output as PNG (lossless)

πŸ“Š Performance

Benchmarks (per image)

Metric Value
Avg Processing Time 1-2 seconds
Memory Usage ~200MB
CPU Usage 50-70%
Max Concurrency 4 workers
Throughput ~30 images/min (single worker)

File Size Limits

Format Max Size
Input 50MB
Output 30MB (PNG)

Supported Formats

Format Input Output
JPEG βœ… βœ…
PNG βœ… βœ…
WebP βœ… βœ…
GIF βœ… βœ…

πŸ”§ Development

See DEVELOPMENT.md for comprehensive development guide including:

  • Code style (PEP 8)
  • Testing strategies
  • Profiling and benchmarking
  • Debugging techniques
  • Release process

Quick Development Setup

git clone https://github.com/Gtajisan/NAFNet-Image-Restoration-API.git
cd NAFNet-Image-Restoration-API
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
python app.py

🀝 Contributing

We welcome contributions! See CONTRIBUTING.md for guidelines.

Quick Contribution Steps

  1. Fork repository
  2. Create feature branch: git checkout -b feature/amazing-feature
  3. Commit changes: git commit -m 'Add amazing feature'
  4. Push branch: git push origin feature/amazing-feature
  5. Open Pull Request

Code Standards

  • Follow PEP 8
  • Add docstrings to functions
  • Write tests for new features
  • Update documentation

πŸ“ License

This project is licensed under the MIT License - see LICENSE file for details.

MIT License allows:

  • βœ… Commercial use
  • βœ… Modification
  • βœ… Distribution
  • βœ… Private use

With conditions:

  • ⚠️ Include license and copyright
  • ⚠️ State changes

πŸ†˜ Support

Getting Help

Common Issues

  • Port already in use: Change port in app.py or use lsof -i :5000
  • Module not found: Run pip install -r requirements.txt
  • Image processing error: Ensure file is valid image format

Security Issues

Report security vulnerabilities to ffjisan804@gmail.com instead of GitHub Issues.


πŸ‘¨β€πŸ’» Author

NAFNet Image Restoration API by Gtajisan


Made with ❀️ by Professional Developers

⭐ If you find this useful, please give it a star on GitHub! ⭐

Star on GitHub Follow on GitHub

Back to Top

Releases

No releases published

Packages

 
 
 

Contributors