Please bookmark this page to avoid losing your image tool!

Peak-Majority Image Effect Generator

(Free & Supports Bulk Upload)

Drag & drop your images here or

The result will appear here...
You can edit the below JavaScript code to customize the image tool.
function processImage(originalImg, radius = "5", intensityLevels = "20") {
    // Parse and sanitize parameters
    let r = parseInt(radius, 10);
    let levels = parseInt(intensityLevels, 10);

    if (isNaN(r) || r < 1) r = 5;
    if (isNaN(levels) || levels < 2) levels = 20;

    // Cap values to prevent freezing the browser on large images
    if (r > 20) r = 20;
    if (levels > 256) levels = 256;

    const canvas = document.createElement('canvas');
    const width = originalImg.width;
    const height = originalImg.height;
    
    // Return empty if image is invalid
    if (width === 0 || height === 0) return canvas;

    canvas.width = width;
    canvas.height = height;

    const ctx = canvas.getContext('2d');
    ctx.drawImage(originalImg, 0, 0);

    const imgData = ctx.getImageData(0, 0, width, height);
    const src = imgData.data;
    const dst = new Uint8ClampedArray(src.length);

    // Pre-allocate intensity bins to be reused
    const intensityCount = new Int32Array(levels + 1);
    const sumR = new Int32Array(levels + 1);
    const sumG = new Int32Array(levels + 1);
    const sumB = new Int32Array(levels + 1);

    // Apply Peak-Majority (Mode/Oil Painting) Filter
    for (let y = 0; y < height; y++) {
        // Optimize bounds to avoid checking them within the nested loops
        let minY = Math.max(0, y - r);
        let maxY = Math.min(height - 1, y + r);

        for (let x = 0; x < width; x++) {
            // Reset bins for current pixel
            intensityCount.fill(0);
            sumR.fill(0);
            sumG.fill(0);
            sumB.fill(0);

            let maxCount = 0;
            let peakIntensity = 0;

            let minX = Math.max(0, x - r);
            let maxX = Math.min(width - 1, x + r);

            // Scan the neighborhood local window
            for (let ny = minY; ny <= maxY; ny++) {
                let yOffset = ny * width;
                
                for (let nx = minX; nx <= maxX; nx++) {
                    let i = (yOffset + nx) * 4;
                    let pr = src[i];
                    let pg = src[i + 1];
                    let pb = src[i + 2];

                    // Convert colors to luminance
                    let luminance = 0.299 * pr + 0.587 * pg + 0.114 * pb;
                    
                    // Determine which intensity bin this belongs to
                    let intensity = Math.floor((luminance * levels) / 255.1);

                    // Add to bin
                    intensityCount[intensity]++;
                    sumR[intensity] += pr;
                    sumG[intensity] += pg;
                    sumB[intensity] += pb;

                    // Keep track of the peak (majority) intensity
                    if (intensityCount[intensity] > maxCount) {
                        maxCount = intensityCount[intensity];
                        peakIntensity = intensity;
                    }
                }
            }

            // Assign the majority color for this pixel
            let outIdx = (y * width + x) * 4;
            dst[outIdx] = sumR[peakIntensity] / maxCount;        // R
            dst[outIdx + 1] = sumG[peakIntensity] / maxCount;    // G
            dst[outIdx + 2] = sumB[peakIntensity] / maxCount;    // B
            dst[outIdx + 3] = src[outIdx + 3];                   // Alpha (kept original)
        }
    }

    // Replace original image data with processed data
    imgData.data.set(dst);
    ctx.putImageData(imgData, 0, 0);

    return canvas;
}

Free Image Tool Creator

Can't find the image tool you're looking for?
Create one based on your own needs now!

Description

The Peak-Majority Image Effect Generator is a tool designed to apply a stylized filtering effect to images, often referred to as an oil painting or mode filter. By analyzing the luminance and color distribution within a specified neighborhood radius, the tool replaces pixel colors with the average color of the most dominant intensity level in that area. This results in a smooth, painterly aesthetic that simplifies textures while preserving major color shapes. It can be used for artistic photo editing, creating stylized profile pictures, or transforming standard photographs into digital art for creative projects.

Leave a Reply

Your email address will not be published. Required fields are marked *