Please bookmark this page to avoid losing your image tool!

Image Background Remover Tool

(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.
async function processImage(originalImg, aiMode = "true", fallbackTolerance = 30, fallbackRemoveIsolated = "false") {
    const width = originalImg.naturalWidth || originalImg.width;
    const height = originalImg.naturalHeight || originalImg.height;
    
    // Attempt 1: State-of-the-Art AI Background Removal (preserves people, objects, text)
    if (aiMode === "true") {
        try {
            // Dynamically import the browser ESM bundle of imgly background removal
            const imgly = await import('https://unpkg.com/@imgly/background-removal@1.4.3/dist/index.browser.mjs');
            const removeBackground = imgly.default || imgly;
            
            // Remove background uses WebAssembly and ONNX models fetched from the public path
            const blob = await removeBackground(originalImg, {
                publicPath: "https://unpkg.com/@imgly/background-removal@1.4.3/dist/"
            });
            
            const url = URL.createObjectURL(blob);
            const resultImg = new Image();
            resultImg.crossOrigin = "anonymous";
            resultImg.src = url;
            
            await new Promise((resolve, reject) => {
                resultImg.onload = resolve;
                resultImg.onerror = reject;
            });
            
            const canvas = document.createElement('canvas');
            canvas.width = resultImg.width;
            canvas.height = resultImg.height;
            const ctx = canvas.getContext('2d');
            ctx.drawImage(resultImg, 0, 0);
            
            URL.revokeObjectURL(url);
            return canvas;
            
        } catch (err) {
            console.warn("AI background removal failed (e.g., due to network or CORS issues). Falling back to heuristic algorithm.", err);
        }
    }
    
    // Attempt 2 (Fallback): Heuristic Edge Flood-Fill / Chroma Key
    const canvas = document.createElement('canvas');
    canvas.width = width;
    canvas.height = height;
    const ctx = canvas.getContext('2d', { willReadFrequently: true });
    ctx.drawImage(originalImg, 0, 0);
    
    try {
        const imgData = ctx.getImageData(0, 0, width, height);
        const data = imgData.data;

        // Sample top-left corner as the background reference color
        const refR = data[0];
        const refG = data[1];
        const refB = data[2];

        // Match based on Euclidean distance
        const colorMatch = (r, g, b) => {
            return Math.sqrt((r - refR) ** 2 + (g - refG) ** 2 + (b - refB) ** 2) <= Number(fallbackTolerance);
        };

        if (fallbackRemoveIsolated === "true") {
            // Chroma Key mode: remove all pixels matching the background color
            for (let i = 0; i < data.length; i += 4) {
                if (colorMatch(data[i], data[i+1], data[i+2])) {
                    data[i+3] = 0;
                }
            }
        } else {
            // Edge flood-fill mode (Magic Wand approach)
            const visited = new Uint8Array(width * height);
            const stack = new Int32Array(width * height * 2);
            let stackPtr = 0;

            // Seed stack with all matching edge pixels
            for (let x = 0; x < width; x++) {
                // Top edge
                if (colorMatch(data[(0 * width + x) * 4], data[(0 * width + x) * 4 + 1], data[(0 * width + x) * 4 + 2])) {
                    stack[stackPtr++] = x;
                    stack[stackPtr++] = 0;
                    visited[0 * width + x] = 1;
                }
                // Bottom edge
                if (colorMatch(data[((height - 1) * width + x) * 4], data[((height - 1) * width + x) * 4 + 1], data[((height - 1) * width + x) * 4 + 2])) {
                    stack[stackPtr++] = x;
                    stack[stackPtr++] = height - 1;
                    visited[(height - 1) * width + x] = 1;
                }
            }
            for (let y = 1; y < height - 1; y++) {
                // Left edge
                if (colorMatch(data[(y * width) * 4], data[(y * width) * 4 + 1], data[(y * width) * 4 + 2])) {
                    stack[stackPtr++] = 0;
                    stack[stackPtr++] = y;
                    visited[y * width] = 1;
                }
                // Right edge
                if (colorMatch(data[(y * width + (width - 1)) * 4], data[(y * width + (width - 1)) * 4 + 1], data[(y * width + (width - 1)) * 4 + 2])) {
                    stack[stackPtr++] = width - 1;
                    stack[stackPtr++] = y;
                    visited[y * width + (width - 1)] = 1;
                }
            }

            const dirs = [[-1, 0], [1, 0], [0, -1], [0, 1]];

            // Perform flood fill iterations
            while(stackPtr > 0) {
                const cy = stack[--stackPtr];
                const cx = stack[--stackPtr];

                const idx = (cy * width + cx) * 4;
                data[idx + 3] = 0; // Set pixel to transparent

                for (let i = 0; i < dirs.length; i++) {
                    const nx = cx + dirs[i][0];
                    const ny = cy + dirs[i][1];

                    if (nx >= 0 && nx < width && ny >= 0 && ny < height) {
                        const nIdx1D = ny * width + nx;
                        if (!visited[nIdx1D]) {
                            visited[nIdx1D] = 1;
                            const nIdx = nIdx1D * 4;
                            if (colorMatch(data[nIdx], data[nIdx+1], data[nIdx+2])) {
                                stack[stackPtr++] = nx;
                                stack[stackPtr++] = ny;
                            }
                        }
                    }
                }
            }
        }
        ctx.putImageData(imgData, 0, 0);
    } catch (e) {
        console.warn("Could not apply heuristic background removal due to Canvas limitations (e.g., Tainted canvas from Cross-Origin image without CORS headers).", e);
    }

    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 users to automatically remove the background from an image, leaving only the main subject. It utilizes advanced AI technology to identify and preserve people, objects, and text, while also providing fallback heuristic methods such as chroma keying and edge flood-fill for different color-matching needs. This tool is ideal for e-commerce product photography, creating professional profile pictures, designing marketing materials, or preparing assets for graphic design projects.

Leave a Reply

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