Please bookmark this page to avoid losing your image tool!

Image Outline Generator With Color Sampling And Anti Aliasing Options

(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, outlineWidth = 5, outlineColor = "black", antiAliasing = "true") {
    if (!originalImg || !originalImg.width || !originalImg.height) {
        return document.createElement('canvas'); // Return empty if invalid
    }

    const widthStr = typeof outlineWidth === 'string' ? parseFloat(outlineWidth) : outlineWidth;
    const R = Math.max(0, widthStr);
    
    // If outline is 0, just return the original on a canvas.
    if (R <= 0) {
        const c = document.createElement('canvas');
        c.width = originalImg.width;
        c.height = originalImg.height;
        const ctx = c.getContext('2d');
        ctx.drawImage(originalImg, 0, 0);
        return c;
    }

    const isAA = String(antiAliasing).toLowerCase() === 'true' || antiAliasing === 1 || antiAliasing === '1';
    
    let mode = 'solid'; // 'solid', 'sample-edge', 'sample-average'
    const colorStr = String(outlineColor).toLowerCase().trim();
    if (colorStr.includes('sample')) {
        if (colorStr.includes('average')) mode = 'sample-average';
        else mode = 'sample-edge'; // default definition for 'sample'
    }

    let r = 0, g = 0, b = 0, a = 255;
    if (mode === 'solid') {
        const tmpCanvas = document.createElement('canvas');
        tmpCanvas.width = 1;
        tmpCanvas.height = 1;
        const tCtx = tmpCanvas.getContext('2d');
        tCtx.fillStyle = outlineColor;
        tCtx.fillRect(0, 0, 1, 1);
        const parsedData = tCtx.getImageData(0, 0, 1, 1).data;
        r = parsedData[0];
        g = parsedData[1];
        b = parsedData[2];
        a = parsedData[3];
    }

    const pad = Math.ceil(R) + 2; 
    const W = originalImg.width + pad * 2;
    const H = originalImg.height + pad * 2;

    const canvas = document.createElement('canvas');
    canvas.width = W;
    canvas.height = H;
    const ctx = canvas.getContext('2d');

    // Draw the original image with padding
    ctx.drawImage(originalImg, pad, pad);

    const imgData = ctx.getImageData(0, 0, W, H);
    const data = imgData.data;
    const size = W * H;

    // 8-point Sequential Euclidean Distance Transform Arrays initialized to "infinity/invalid"
    const nearestX = new Int16Array(size);
    const nearestY = new Int16Array(size);
    nearestX.fill(-10000);
    nearestY.fill(-10000);

    let sumR = 0, sumG = 0, sumB = 0, count = 0;
    const threshold = 127;

    // Collect seeds (pixels part of original image silhouette)
    for (let i = 0; i < size; i++) {
        if (data[i * 4 + 3] >= threshold) {
            nearestX[i] = i % W;
            nearestY[i] = i / W | 0;
            
            if (mode === 'sample-average') {
                sumR += data[i * 4];
                sumG += data[i * 4 + 1];
                sumB += data[i * 4 + 2];
                count++;
            }
        }
    }

    // Resolve average fallback
    if (mode === 'sample-average') {
        r = count > 0 ? Math.round(sumR / count) : 0;
        g = count > 0 ? Math.round(sumG / count) : 0;
        b = count > 0 ? Math.round(sumB / count) : 0;
        mode = 'solid'; // We collapse modes so drawing behaves cleanly like solid
    }

    // Forward Pass (Top-Left to Bottom-Right)
    for (let y = 0; y < H; y++) {
        for (let x = 0; x < W; x++) {
            const idx = y * W + x;
            let cx = nearestX[idx];
            let cy = nearestY[idx];
            let bestDistSq = cx === -10000 ? 1e9 : (cx - x) * (cx - x) + (cy - y) * (cy - y);

            // x-1, y-1
            if (x > 0 && y > 0) {
                const n = idx - W - 1;
                const sx = nearestX[n];
                if (sx !== -10000) {
                    const sy = nearestY[n];
                    const d = (sx - x) * (sx - x) + (sy - y) * (sy - y);
                    if (d < bestDistSq) { bestDistSq = d; cx = sx; cy = sy; }
                }
            }
            // x, y-1
            if (y > 0) {
                const n = idx - W;
                const sx = nearestX[n];
                if (sx !== -10000) {
                    const sy = nearestY[n];
                    const d = (sx - x) * (sx - x) + (sy - y) * (sy - y);
                    if (d < bestDistSq) { bestDistSq = d; cx = sx; cy = sy; }
                }
            }
            // x+1, y-1
            if (x < W - 1 && y > 0) {
                const n = idx - W + 1;
                const sx = nearestX[n];
                if (sx !== -10000) {
                    const sy = nearestY[n];
                    const d = (sx - x) * (sx - x) + (sy - y) * (sy - y);
                    if (d < bestDistSq) { bestDistSq = d; cx = sx; cy = sy; }
                }
            }
            // x-1, y
            if (x > 0) {
                const n = idx - 1;
                const sx = nearestX[n];
                if (sx !== -10000) {
                    const sy = nearestY[n];
                    const d = (sx - x) * (sx - x) + (sy - y) * (sy - y);
                    if (d < bestDistSq) { bestDistSq = d; cx = sx; cy = sy; }
                }
            }

            nearestX[idx] = cx;
            nearestY[idx] = cy;
        }
    }

    // Reverse Pass (Bottom-Right to Top-Left)
    for (let y = H - 1; y >= 0; y--) {
        for (let x = W - 1; x >= 0; x--) {
            const idx = y * W + x;
            let cx = nearestX[idx];
            let cy = nearestY[idx];
            let bestDistSq = cx === -10000 ? 1e9 : (cx - x) * (cx - x) + (cy - y) * (cy - y);

            // x+1, y+1
            if (x < W - 1 && y < H - 1) {
                const n = idx + W + 1;
                const sx = nearestX[n];
                if (sx !== -10000) {
                    const sy = nearestY[n];
                    const d = (sx - x) * (sx - x) + (sy - y) * (sy - y);
                    if (d < bestDistSq) { bestDistSq = d; cx = sx; cy = sy; }
                }
            }
            // x, y+1
            if (y < H - 1) {
                const n = idx + W;
                const sx = nearestX[n];
                if (sx !== -10000) {
                    const sy = nearestY[n];
                    const d = (sx - x) * (sx - x) + (sy - y) * (sy - y);
                    if (d < bestDistSq) { bestDistSq = d; cx = sx; cy = sy; }
                }
            }
            // x-1, y+1
            if (x > 0 && y < H - 1) {
                const n = idx + W - 1;
                const sx = nearestX[n];
                if (sx !== -10000) {
                    const sy = nearestY[n];
                    const d = (sx - x) * (sx - x) + (sy - y) * (sy - y);
                    if (d < bestDistSq) { bestDistSq = d; cx = sx; cy = sy; }
                }
            }
            // x+1, y
            if (x < W - 1) {
                const n = idx + 1;
                const sx = nearestX[n];
                if (sx !== -10000) {
                    const sy = nearestY[n];
                    const d = (sx - x) * (sx - x) + (sy - y) * (sy - y);
                    if (d < bestDistSq) { bestDistSq = d; cx = sx; cy = sy; }
                }
            }

            nearestX[idx] = cx;
            nearestY[idx] = cy;
        }
    }

    // Paint the Outline buffer using resulting distances
    const outDataArray = new Uint8ClampedArray(size * 4);
    for (let i = 0; i < size; i++) {
        const sx = nearestX[i];
        if (sx !== -10000) {
            const sy = nearestY[i];
            const x = i % W;
            const y = i / W | 0;
            const distSq = (sx - x) * (sx - x) + (sy - y) * (sy - y);

            if (distSq <= (R + 1) * (R + 1)) {
                const dist = Math.sqrt(distSq);

                if (dist <= R + (isAA ? 1 : 0)) {
                    let outR = r, outG = g, outB = b, outA = 255;

                    if (mode === 'sample-edge') {
                        const sIdx = (sy * W + sx) * 4;
                        outR = data[sIdx];
                        outG = data[sIdx + 1];
                        outB = data[sIdx + 2];
                    }

                    if (isAA) {
                        if (dist > R) {
                            outA = Math.max(0, Math.min(255, 255 * (R + 1 - dist)));
                        }
                    } else {
                        if (dist > R) {
                            outA = 0;
                        }
                    }

                    if (outA > 0) {
                        outDataArray[i * 4] = outR;
                        outDataArray[i * 4 + 1] = outG;
                        outDataArray[i * 4 + 2] = outB;
                        outDataArray[i * 4 + 3] = outA;
                    }
                }
            }
        }
    }

    // Write outline visually then overlay fully-original image (so original edge details are kept intact)
    const outImg = new ImageData(outDataArray, W, H);
    ctx.clearRect(0, 0, W, H);
    ctx.putImageData(outImg, 0, 0);

    // Overlay Original Image centered
    ctx.drawImage(originalImg, pad, pad);

    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

This tool allows you to generate a custom outline around the subject of an image. Users can adjust the thickness of the outline and choose between a solid color or color sampling modes that pull colors from the image’s edges or its average color profile. It also includes an anti-aliasing option to ensure the outlines appear smooth rather than pixelated. This tool is useful for graphic designers creating stickers, digital artists preparing assets for layering, or anyone looking to make subjects stand out more clearly in visual compositions.

Leave a Reply

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