Please bookmark this page to avoid losing your image tool!

Salt On Skin Photo Effect Adder

(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, saltDensity = 0.02, maxSaltSize = 3, focusOnSkin = 1) {
    // Ensure parameters are parsed as numbers 
    saltDensity = Number(saltDensity) || 0.02;
    maxSaltSize = Number(maxSaltSize) || 3;
    focusOnSkin = Number(focusOnSkin);
    if (isNaN(focusOnSkin)) focusOnSkin = 1;

    const canvas = document.createElement('canvas');
    canvas.width = originalImg.width;
    canvas.height = originalImg.height;
    const ctx = canvas.getContext('2d');
    
    // Draw original image to extract pixel data
    ctx.drawImage(originalImg, 0, 0);
    const imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
    const data = imgData.data;
    
    // Keep an unmodified copy of the data to evaluate skin detection
    const originalData = new Uint8ClampedArray(data);
    
    // Helper function to draw a single salt grain (cluster of pixels)
    const addSalt = (x, y, size) => {
        const half = Math.floor(size / 2);
        for (let dy = 0; dy < size; dy++) {
            for (let dx = 0; dx < size; dx++) {
                const px = x + dx - half;
                const py = y + dy - half;
                
                // Stay within canvas bounds
                if (px >= 0 && px < canvas.width && py >= 0 && py < canvas.height) {
                    const idx = (py * canvas.width + px) * 4;
                    // Add some randomization to the salt opacity for a natural, crystalline look
                    const op = 0.4 + Math.random() * 0.6;
                    
                    data[idx] += (255 - data[idx]) * op;       // R
                    data[idx+1] += (255 - data[idx+1]) * op;   // G
                    data[idx+2] += (255 - data[idx+2]) * op;   // B
                    // Alpha channel remains the same
                }
            }
        }
    };

    // Iterate through all pixels of the image
    for (let y = 0; y < canvas.height; y++) {
        for (let x = 0; x < canvas.width; x++) {
            const idx = (y * canvas.width + x) * 4;
            const r = originalData[idx];
            const g = originalData[idx+1];
            const b = originalData[idx+2];
            
            let skinMatch = true;
            
            // Apply heuristic-based skin detection if focusOnSkin is enabled
            if (focusOnSkin === 1) {
                const max = Math.max(r, g, b);
                const min = Math.min(r, g, b);
                
                // Typical conditions for detecting skin tones under basic lighting
                const isSkin = r > 95 && g > 40 && b > 20 && 
                               (max - min) > 15 && 
                               Math.abs(r - g) > 15 && 
                               r > g && r > b;
                               
                // Fallback for darker or shadowed skin tones
                const isDarkSkin = r > 40 && g > 20 && b > 10 &&
                                   (max - min) > 10 &&
                                   Math.abs(r - g) > 10 &&
                                   r > g && r >= b;
                                   
                skinMatch = isSkin || isDarkSkin;
            }
            
            // Adjust the salt presence based on skin-detection results
            let currentDensity = saltDensity;
            if (focusOnSkin === 1 && !skinMatch) {
                // Diminish salt drastically on non-skin pixels (e.g. background, clothing)
                currentDensity *= 0.05;
            }
            
            // Randomly seed salt grains
            if (Math.random() < currentDensity) {
                const s = Math.floor(Math.random() * maxSaltSize) + 1;
                addSalt(x, y, s);
            }
        }
    }
    
    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 Salt On Skin Photo Effect Adder is an image processing tool designed to apply a realistic salt-grain texture to photographs. It features intelligent skin detection capabilities that allow the effect to be concentrated on skin tones while minimizing its appearance on backgrounds or clothing. This tool is ideal for creators looking to add texture, grit, or a specific aesthetic finish to portraits and lifestyle photography.

Leave a Reply

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