API Documentation
The K-Means Image Compressor provides a RESTful API for programmatic image compression. All endpoints return JSON responses and support CORS.
Base URL
https://your-domain.com/api
This API is currently open and doesn't require authentication. Rate limiting may apply.
Health Check
Check if the API service is running and healthy.
GET
/api/health
Response
{
"status": "healthy",
"timestamp": "2024-01-15T10:30:00.000Z",
"version": "1.0.0"
}
Upload Image
Upload an image for later compression. Returns an image ID for subsequent operations.
POST
/api/upload
Request
| Parameter | Type | Description |
|---|---|---|
image |
File (multipart/form-data) | Image file (PNG, JPG, JPEG, GIF, BMP, WebP). Max 16MB. |
cURL Example
curl -X POST \
-F "image=@photo.jpg" \
https://your-domain.com/api/upload
Response
{
"success": true,
"image_id": "20240115_103000_a1b2c3d4.jpg",
"preview_url": "https://your-domain.com/api/original/20240115_103000_a1b2c3d4.jpg",
"dimensions": {
"width": 1920,
"height": 1080
},
"format": "JPEG",
"file_size": 524288
}
Compress Image
Compress a previously uploaded image using K-Means clustering.
POST
/api/compress
Request Body (JSON)
| Parameter | Type | Default | Description |
|---|---|---|---|
image_id * |
string | - | ID of the uploaded image |
n_colors |
integer | 16 | Number of colors (2-256) |
max_iters |
integer | 10 | Maximum iterations (1-100) |
quality |
integer | 95 | Output JPEG quality (1-100) |
cURL Example
curl -X POST \
-H "Content-Type: application/json" \
-d '{"image_id": "20240115_103000_a1b2c3d4.jpg", "n_colors": 16}' \
https://your-domain.com/api/compress
Response
{
"success": true,
"compressed_id": "compressed_20240115_103000_a1b2c3d4.jpg",
"download_url": "https://your-domain.com/api/download/compressed_...",
"preview_url": "https://your-domain.com/api/compressed/compressed_...",
"stats": {
"original_file_size": 524288,
"compressed_file_size": 156432,
"file_compression_ratio": 3.35,
"theoretical_compression_ratio": 6.0,
"original_colors": 45821,
"reduced_colors": 16,
"iterations": 8,
"processing_time_ms": 1234.56
}
}
Compress Direct
Upload and compress an image in a single request. Returns the compressed image directly.
POST
/api/compress-direct
Request (multipart/form-data)
| Parameter | Type | Default | Description |
|---|---|---|---|
image * |
File | - | Image file to compress |
n_colors |
integer | 16 | Number of colors |
max_iters |
integer | 10 | Maximum iterations |
cURL Example
curl -X POST \
-F "image=@photo.jpg" \
-F "n_colors=16" \
-o compressed.png \
https://your-domain.com/api/compress-direct
Response
Returns the compressed image file directly (image/png).
Get Presets
Get predefined compression presets for common use cases.
GET
/api/presets
Response
{
"success": true,
"presets": [
{
"name": "Maximum Compression",
"description": "Smallest file size, reduced quality",
"n_colors": 4,
"max_iters": 15
},
{
"name": "Balanced",
"description": "Good balance of size and quality",
"n_colors": 16,
"max_iters": 10
},
...
]
}
Error Handling
All API errors return a consistent JSON format with an error message.
HTTP Status Codes
| Code | Description |
|---|---|
200 |
Success |
400 |
Bad Request - Invalid parameters |
404 |
Not Found - Resource doesn't exist |
413 |
Payload Too Large - File exceeds 16MB |
500 |
Internal Server Error |
Error Response Format
{
"success": false,
"error": "Descriptive error message"
}
Python Example
import requests
# Upload and compress an image
def compress_image(image_path, n_colors=16):
base_url = "https://your-domain.com/api"
# Upload
with open(image_path, 'rb') as f:
response = requests.post(
f"{base_url}/upload",
files={'image': f}
)
image_id = response.json()['image_id']
# Compress
response = requests.post(
f"{base_url}/compress",
json={
'image_id': image_id,
'n_colors': n_colors
}
)
result = response.json()
# Download
download_url = result['download_url']
img_response = requests.get(download_url)
with open('compressed.png', 'wb') as f:
f.write(img_response.content)
return result['stats']
# Usage
stats = compress_image('photo.jpg', n_colors=16)
print(f"Compression ratio: {stats['file_compression_ratio']}x")
JavaScript Example
async function compressImage(file, nColors = 16) {
const baseUrl = 'https://your-domain.com/api';
// Upload
const formData = new FormData();
formData.append('image', file);
const uploadResponse = await fetch(`${baseUrl}/upload`, {
method: 'POST',
body: formData
});
const { image_id } = await uploadResponse.json();
// Compress
const compressResponse = await fetch(`${baseUrl}/compress`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
image_id,
n_colors: nColors
})
});
return await compressResponse.json();
}
// Usage
const fileInput = document.querySelector('input[type="file"]');
fileInput.addEventListener('change', async (e) => {
const result = await compressImage(e.target.files[0], 16);
console.log(`Compressed! Ratio: ${result.stats.file_compression_ratio}x`);
});