A comprehensive guide to image processing - digital techniques for analyzing, enhancing, and transforming images, covering filters, transformations, color manipulation, and automated processing workflows.
Image processing encompasses digital techniques for analyzing, enhancing, and transforming images. It's fundamental to computer vision, digital photography, web development, and any application that manipulates visual data programmatically.
Transform your images with cutting-edge AI tools designed for photographers, designers, and content creators.
Intelligent resizing algorithms • Instant background removal • Preserve image quality • Batch processing support
Basic operations that modify individual pixels or apply transformations across the entire image:
// Basic image processing operations
class ImageProcessor {
constructor() {
this.canvas = document.createElement('canvas');
this.ctx = this.canvas.getContext('2d');
}
// Brightness adjustment
adjustBrightness(imageData, amount) {
const { data, width, height } = imageData;
const result = new Uint8ClampedArray(data.length);
for (let i = 0; i < data.length; i += 4) {
result[i] = Math.max(0, Math.min(255, data[i] + amount)); // Red
result[i + 1] = Math.max(0, Math.min(255, data[i + 1] + amount)); // Green
result[i + 2] = Math.max(0, Math.min(255, data[i + 2] + amount)); // Blue
result[i + 3] = data[i + 3]; // Alpha unchanged
}
return new ImageData(result, width, height);
}
// Contrast adjustment
adjustContrast(imageData, factor) {
const { data, width, height } = imageData;
const result = new Uint8ClampedArray(data.length);
for (let i = 0; i < data.length; i += 4) {
result[i] = Math.max(0, Math.min(255, (data[i] - 128) * factor + 128));
result[i + 1] = Math.max(0, Math.min(255, (data[i + 1] - 128) * factor + 128));
result[i + 2] = Math.max(0, Math.min(255, (data[i + 2] - 128) * factor + 128));
result[i + 3] = data[i + 3];
}
return new ImageData(result, width, height);
}
// Saturation adjustment
adjustSaturation(imageData, factor) {
const { data, width, height } = imageData;
const result = new Uint8ClampedArray(data.length);
for (let i = 0; i < data.length; i += 4) {
const r = data[i];
const g = data[i + 1];
const b = data[i + 2];
// Convert to grayscale
const gray = 0.299 * r + 0.587 * g + 0.114 * b;
// Apply saturation
result[i] = Math.max(0, Math.min(255, gray + factor * (r - gray)));
result[i + 1] = Math.max(0, Math.min(255, gray + factor * (g - gray)));
result[i + 2] = Math.max(0, Math.min(255, gray + factor * (b - gray)));
result[i + 3] = data[i + 3];
}
return new ImageData(result, width, height);
}
// Hue shift
adjustHue(imageData, degrees) {
const { data, width, height } = imageData;
const result = new Uint8ClampedArray(data.length);
const radians = degrees * Math.PI / 180;
for (let i = 0; i < data.length; i += 4) {
const r = data[i] / 255;
const g = data[i + 1] / 255;
const b = data[i + 2] / 255;
const hsl = this.rgbToHsl(r, g, b);
hsl.h = (hsl.h + degrees) % 360;
if (hsl.h < 0) hsl.h += 360;
const rgb = this.hslToRgb(hsl.h, hsl.s, hsl.l);
result[i] = Math.round(rgb.r * 255);
result[i + 1] = Math.round(rgb.g * 255);
result[i + 2] = Math.round(rgb.b * 255);
result[i + 3] = data[i + 3];
}
return new ImageData(result, width, height);
}
rgbToHsl(r, g, b) {
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
const diff = max - min;
const sum = max + min;
const l = sum / 2;
if (diff === 0) {
return { h: 0, s: 0, l };
}
const s = l > 0.5 ? diff / (2 - sum) : diff / sum;
let h;
switch (max) {
case r: h = ((g - b) / diff + (g < b ? 6 : 0)) / 6; break;
case g: h = ((b - r) / diff + 2) / 6; break;
case b: h = ((r - g) / diff + 4) / 6; break;
}
return { h: h * 360, s, l };
}
hslToRgb(h, s, l) {
h /= 360;
if (s === 0) {
return { r: l, g: l, b: l };
}
const hue2rgb = (p, q, t) => {
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1/6) return p + (q - p) * 6 * t;
if (t < 1/2) return q;
if (t < 2/3) return p + (q - p) * (2/3 - t) * 6;
return p;
};
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
const p = 2 * l - q;
return {
r: hue2rgb(p, q, h + 1/3),
g: hue2rgb(p, q, h),
b: hue2rgb(p, q, h - 1/3)
};
}
}// Convolution-based image filters
class ImageFilters {
constructor() {
this.kernels = {
blur: [
[1/9, 1/9, 1/9],
[1/9, 1/9, 1/9],
[1/9, 1/9, 1/9]
],
sharpen: [
[0, -1, 0],
[-1, 5, -1],
[0, -1, 0]
],
edgeDetection: [
[-1, -1, -1],
[-1, 8, -1],
[-1, -1, -1]
],
emboss: [
[-2, -1, 0],
[-1, 1, 1],
[0, 1, 2]
]
};
}
// Apply convolution filter
applyConvolution(imageData, kernel) {
const { data, width, height } = imageData;
const result = new Uint8ClampedArray(data.length);
const kernelSize = kernel.length;
const half = Math.floor(kernelSize / 2);
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
let r = 0, g = 0, b = 0;
// Apply kernel
for (let ky = 0; ky < kernelSize; ky++) {
for (let kx = 0; kx < kernelSize; kx++) {
const px = Math.min(width - 1, Math.max(0, x + kx - half));
const py = Math.min(height - 1, Math.max(0, y + ky - half));
const idx = (py * width + px) * 4;
const weight = kernel[ky][kx];
r += data[idx] * weight;
g += data[idx + 1] * weight;
b += data[idx + 2] * weight;
}
}
const idx = (y * width + x) * 4;
result[idx] = Math.max(0, Math.min(255, r));
result[idx + 1] = Math.max(0, Math.min(255, g));
result[idx + 2] = Math.max(0, Math.min(255, b));
result[idx + 3] = data[idx + 3];
}
}
return new ImageData(result, width, height);
}
// Gaussian blur
gaussianBlur(imageData, radius = 1) {
const kernel = this.generateGaussianKernel(radius);
return this.applyConvolution(imageData, kernel);
}
generateGaussianKernel(radius) {
const size = radius * 2 + 1;
const kernel = [];
const sigma = radius / 3;
let sum = 0;
// Generate kernel
for (let y = 0; y < size; y++) {
kernel[y] = [];
for (let x = 0; x < size; x++) {
const dx = x - radius;
const dy = y - radius;
const value = Math.exp(-(dx * dx + dy * dy) / (2 * sigma * sigma));
kernel[y][x] = value;
sum += value;
}
}
// Normalize kernel
for (let y = 0; y < size; y++) {
for (let x = 0; x < size; x++) {
kernel[y][x] /= sum;
}
}
return kernel;
}
// Unsharp mask
unsharpMask(imageData, amount = 1.5, radius = 1, threshold = 0) {
const blurred = this.gaussianBlur(imageData, radius);
const { data: original, width, height } = imageData;
const { data: blurredData } = blurred;
const result = new Uint8ClampedArray(original.length);
for (let i = 0; i < original.length; i += 4) {
for (let c = 0; c < 3; c++) {
const orig = original[i + c];
const blur = blurredData[i + c];
const diff = orig - blur;
if (Math.abs(diff) > threshold) {
result[i + c] = Math.max(0, Math.min(255, orig + diff * amount));
} else {
result[i + c] = orig;
}
}
result[i + 3] = original[i + 3];
}
return new ImageData(result, width, height);
}
}// Color space conversion for image processing
class ColorSpaceProcessor {
constructor() {
this.processor = new ImageProcessor();
}
// Convert image to grayscale
toGrayscale(imageData, method = 'luminance') {
const { data, width, height } = imageData;
const result = new Uint8ClampedArray(data.length);
for (let i = 0; i < data.length; i += 4) {
const r = data[i];
const g = data[i + 1];
const b = data[i + 2];
let gray;
switch (method) {
case 'average':
gray = (r + g + b) / 3;
break;
case 'luminance':
gray = 0.299 * r + 0.587 * g + 0.114 * b;
break;
case 'desaturation':
gray = (Math.max(r, g, b) + Math.min(r, g, b)) / 2;
break;
default:
gray = 0.299 * r + 0.587 * g + 0.114 * b;
}
result[i] = gray;
result[i + 1] = gray;
result[i + 2] = gray;
result[i + 3] = data[i + 3];
}
return new ImageData(result, width, height);
}
// Sepia effect
applySepia(imageData) {
const { data, width, height } = imageData;
const result = new Uint8ClampedArray(data.length);
for (let i = 0; i < data.length; i += 4) {
const r = data[i];
const g = data[i + 1];
const b = data[i + 2];
result[i] = Math.min(255, (r * 0.393) + (g * 0.769) + (b * 0.189));
result[i + 1] = Math.min(255, (r * 0.349) + (g * 0.686) + (b * 0.168));
result[i + 2] = Math.min(255, (r * 0.272) + (g * 0.534) + (b * 0.131));
result[i + 3] = data[i + 3];
}
return new ImageData(result, width, height);
}
// Color temperature adjustment
adjustColorTemperature(imageData, temperature) {
const { data, width, height } = imageData;
const result = new Uint8ClampedArray(data.length);
// Temperature adjustment factors
const factor = temperature / 6500; // 6500K is neutral
const rFactor = factor > 1 ? 1 : factor;
const bFactor = factor < 1 ? 1 : 1 / factor;
for (let i = 0; i < data.length; i += 4) {
result[i] = Math.max(0, Math.min(255, data[i] * rFactor));
result[i + 1] = data[i + 1]; // Green unchanged
result[i + 2] = Math.max(0, Math.min(255, data[i + 2] * bFactor));
result[i + 3] = data[i + 3];
}
return new ImageData(result, width, height);
}
// White balance correction
correctWhiteBalance(imageData, whitePoint) {
const { r: wr, g: wg, b: wb } = whitePoint;
const { data, width, height } = imageData;
const result = new Uint8ClampedArray(data.length);
// Calculate correction factors
const rFactor = 255 / wr;
const gFactor = 255 / wg;
const bFactor = 255 / wb;
for (let i = 0; i < data.length; i += 4) {
result[i] = Math.max(0, Math.min(255, data[i] * rFactor));
result[i + 1] = Math.max(0, Math.min(255, data[i + 1] * gFactor));
result[i + 2] = Math.max(0, Math.min(255, data[i + 2] * bFactor));
result[i + 3] = data[i + 3];
}
return new ImageData(result, width, height);
}
}// Geometric image transformations
class GeometricProcessor {
constructor() {
this.canvas = document.createElement('canvas');
this.ctx = this.canvas.getContext('2d');
}
// Resize image with quality preservation
resizeImage(imageFile, newWidth, newHeight, quality = 'high') {
return new Promise((resolve) => {
const img = new Image();
img.onload = () => {
this.canvas.width = newWidth;
this.canvas.height = newHeight;
// Set image smoothing based on quality
this.ctx.imageSmoothingEnabled = quality !== 'pixelated';
this.ctx.imageSmoothingQuality = quality === 'high' ? 'high' : 'medium';
this.ctx.drawImage(img, 0, 0, newWidth, newHeight);
this.canvas.toBlob((blob) => {
resolve({
blob,
originalDimensions: { width: img.width, height: img.height },
newDimensions: { width: newWidth, height: newHeight },
scaleFactor: { x: newWidth / img.width, y: newHeight / img.height }
});
}, 'image/png');
};
img.src = URL.createObjectURL(imageFile);
});
}
// Rotate image
rotateImage(imageFile, degrees) {
return new Promise((resolve) => {
const img = new Image();
img.onload = () => {
const radians = degrees * Math.PI / 180;
const cos = Math.abs(Math.cos(radians));
const sin = Math.abs(Math.sin(radians));
// Calculate new dimensions
const newWidth = img.width * cos + img.height * sin;
const newHeight = img.width * sin + img.height * cos;
this.canvas.width = newWidth;
this.canvas.height = newHeight;
// Move to center and rotate
this.ctx.translate(newWidth / 2, newHeight / 2);
this.ctx.rotate(radians);
this.ctx.drawImage(img, -img.width / 2, -img.height / 2);
this.canvas.toBlob((blob) => {
resolve({
blob,
rotation: degrees,
originalDimensions: { width: img.width, height: img.height },
newDimensions: { width: newWidth, height: newHeight }
});
}, 'image/png');
};
img.src = URL.createObjectURL(imageFile);
});
}
// Crop image
cropImage(imageFile, x, y, width, height) {
return new Promise((resolve) => {
const img = new Image();
img.onload = () => {
this.canvas.width = width;
this.canvas.height = height;
this.ctx.drawImage(img, x, y, width, height, 0, 0, width, height);
this.canvas.toBlob((blob) => {
resolve({
blob,
cropArea: { x, y, width, height },
originalDimensions: { width: img.width, height: img.height }
});
}, 'image/png');
};
img.src = URL.createObjectURL(imageFile);
});
}
}// Automated image processing pipeline
class ImageProcessingPipeline {
constructor() {
this.processor = new ImageProcessor();
this.filters = new ImageFilters();
this.colorProcessor = new ColorSpaceProcessor();
this.geometricProcessor = new GeometricProcessor();
}
// Process multiple images with same operations
async batchProcess(imageFiles, operations) {
const results = [];
for (const file of imageFiles) {
try {
const result = await this.processImage(file, operations);
results.push({
originalFile: file.name,
success: true,
result: result
});
} catch (error) {
results.push({
originalFile: file.name,
success: false,
error: error.message
});
}
}
return results;
}
// Process single image with operation chain
async processImage(imageFile, operations) {
let currentBlob = imageFile;
const history = [];
for (const operation of operations) {
const result = await this.applyOperation(currentBlob, operation);
currentBlob = result.blob;
history.push({
operation: operation.type,
parameters: operation.params,
size: result.blob.size
});
}
return {
finalImage: currentBlob,
processingHistory: history,
originalSize: imageFile.size,
finalSize: currentBlob.size,
compressionRatio: (1 - currentBlob.size / imageFile.size) * 100
};
}
async applyOperation(imageBlob, operation) {
const { type, params } = operation;
switch (type) {
case 'resize':
return await this.geometricProcessor.resizeImage(
imageBlob,
params.width,
params.height,
params.quality
);
case 'rotate':
return await this.geometricProcessor.rotateImage(
imageBlob,
params.degrees
);
case 'crop':
return await this.geometricProcessor.cropImage(
imageBlob,
params.x,
params.y,
params.width,
params.height
);
case 'brightness':
return await this.applyImageDataOperation(
imageBlob,
(imageData) => this.processor.adjustBrightness(imageData, params.amount)
);
case 'contrast':
return await this.applyImageDataOperation(
imageBlob,
(imageData) => this.processor.adjustContrast(imageData, params.factor)
);
case 'saturation':
return await this.applyImageDataOperation(
imageBlob,
(imageData) => this.processor.adjustSaturation(imageData, params.factor)
);
case 'blur':
return await this.applyImageDataOperation(
imageBlob,
(imageData) => this.filters.gaussianBlur(imageData, params.radius)
);
case 'sharpen':
return await this.applyImageDataOperation(
imageBlob,
(imageData) => this.filters.applyConvolution(imageData, this.filters.kernels.sharpen)
);
case 'grayscale':
return await this.applyImageDataOperation(
imageBlob,
(imageData) => this.colorProcessor.toGrayscale(imageData, params.method)
);
default:
throw new Error(`Unknown operation: ${type}`);
}
}
async applyImageDataOperation(imageBlob, operation) {
return new Promise((resolve) => {
const img = new Image();
img.onload = () => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
const imageData = ctx.getImageData(0, 0, img.width, img.height);
const processedData = operation(imageData);
ctx.putImageData(processedData, 0, 0);
canvas.toBlob((blob) => {
resolve({ blob });
}, 'image/png');
};
img.src = URL.createObjectURL(imageBlob);
});
}
// Preset processing workflows
getPresetWorkflows() {
return {
webOptimization: [
{ type: 'resize', params: { width: 1200, height: 800, quality: 'high' } },
{ type: 'contrast', params: { factor: 1.1 } },
{ type: 'saturation', params: { factor: 1.05 } }
],
thumbnailGeneration: [
{ type: 'resize', params: { width: 300, height: 300, quality: 'medium' } },
{ type: 'sharpen', params: {} }
],
vintageEffect: [
{ type: 'saturation', params: { factor: 0.8 } },
{ type: 'brightness', params: { amount: -10 } },
{ type: 'contrast', params: { factor: 1.2 } }
],
blackAndWhite: [
{ type: 'grayscale', params: { method: 'luminance' } },
{ type: 'contrast', params: { factor: 1.3 } }
]
};
}
}Explore these comprehensive pillar articles related to Image Processing
A comprehensive guide to monochromatic color schemes - sophisticated single-hue harmonies that create unified visual experiences through systematic variations in saturation, lightness, and tonal relationships, essential for elegant design compositions, brand consistency, and advanced color theory applications requiring cohesive aesthetic unity.
A comprehensive guide to color theory, its fundamental principles, and practical applications in digital design and web development.
Comprehensive guide to complementary colors - opposing hues on the color wheel that create maximum visual contrast, dynamic tension, and powerful design impact through scientific color relationships and perceptual phenomena.
Comprehensive guide to analogous color schemes - adjacent hues on the color wheel that create natural harmony, visual cohesion, and sophisticated design solutions through subtle color relationships and graduated transitions.
Discover more about Image Processing and related color theory concepts
A comprehensive guide to monochromatic color schemes - sophisticated single-hue harmonies that create unified visual experiences through systematic variations in saturation, lightness, and tonal relationships, essential for elegant design compositions, brand consistency, and advanced color theory applications requiring cohesive aesthetic unity.
Comprehensive guide to analogous color schemes - adjacent hues on the color wheel that create natural harmony, visual cohesion, and sophisticated design solutions through subtle color relationships and graduated transitions.
A comprehensive guide to color theory, its fundamental principles, and practical applications in digital design and web development.
Comprehensive guide to complementary colors - opposing hues on the color wheel that create maximum visual contrast, dynamic tension, and powerful design impact through scientific color relationships and perceptual phenomena.
A comprehensive guide to split-complementary color schemes - sophisticated three-color harmonies that combine a base color with two colors adjacent to its complement, creating balanced visual contrast with reduced tension compared to direct complementary pairs, essential for refined design compositions, brand identity systems, and advanced color theory applications.
A comprehensive guide to tetradic color schemes - sophisticated four-color harmonies that use two complementary pairs arranged in rectangular or square formations on the color wheel, providing maximum color variety while maintaining structured relationships essential for dynamic design compositions, brand systems, and advanced color theory applications.
Disclosure: The links below are Amazon affiliate links. RGBmatic earns a commission from qualifying purchases at no extra cost to you.
Discover more color terminology and concepts in our comprehensive glossary.
Browse Full Glossary