Please bookmark this page to avoid losing your image tool!

Image Diffusion Filter Effect 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.
function processImage(originalImg, strength = 5) {
    // Use naturalWidth and naturalHeight for intrinsic image dimensions
    const width = originalImg.naturalWidth || originalImg.width;
    const height = originalImg.naturalHeight || originalImg.height;

    const canvas = document.createElement('canvas');
    canvas.width = width;
    canvas.height = height;

    // If image dimensions are zero (e.g., image not loaded or is empty),
    // return an empty canvas of the correct (zero) size.
    if (width === 0 || height === 0) {
        return canvas;
    }

    const ctx = canvas.getContext('2d');
    if (!ctx) {
        // This should ideally not happen in modern browsers for a 2D context
        console.error("Failed to get 2D context from canvas.");
        // Return the canvas, which will be blank
        return canvas;
    }

    // Draw the original image onto the canvas. This allows us to read its pixel data.
    // Specify width and height in drawImage to ensure it fills the canvas,
    // especially if originalImg.width/height attributes were set by CSS affecting its display size.
    ctx.drawImage(originalImg, 0, 0, width, height);

    // Validate and normalize the 'strength' parameter.
    // It should be a non-negative number.
    let numStrength = Number(strength);
    if (isNaN(numStrength) || numStrength < 0) {
        numStrength = 0; // Default to 0 (no effect) if strength is invalid or negative
    }
    
    // If strength is 0, no diffusion effect needs to be applied.
    // The canvas already contains the original image, so we can return it directly.
    if (numStrength === 0) {
        return canvas;
    }

    // Get the pixel data of the image drawn on the canvas.
    // Note: This can throw a SecurityError if the image is cross-origin and taints the canvas.
    let sourceImageData;
    try {
        sourceImageData = ctx.getImageData(0, 0, width, height);
    } catch (e) {
        console.error("Error getting ImageData: ", e);
        // Could be a CORS issue. Return the canvas with the original image drawn, without the effect.
        return canvas;
    }
    
    const sourceData = sourceImageData.data;

    // Create a new ImageData object to hold the processed pixel data.
    const targetImageData = ctx.createImageData(width, height);
    const targetData = targetImageData.data;

    // Iterate over each pixel in the image.
    for (let y = 0; y < height; y++) {
        for (let x = 0; x < width; x++) {
            // Calculate random offsets for diffusion.
            // (Math.random() - 0.5) results in a range from -0.5 to +0.5.
            // Multiplying by 2 gives a range from -1.0 to +1.0.
            // This is then scaled by numStrength to control the diffusion intensity.
            const offsetX = (Math.random() - 0.5) * 2 * numStrength;
            const offsetY = (Math.random() - 0.5) * 2 * numStrength;

            // Calculate the source pixel coordinates to sample from by adding the random offset.
            // Round to the nearest integer for pixel coordinates.
            let sampleX = Math.round(x + offsetX);
            let sampleY = Math.round(y + offsetY);

            // Clamp the sample coordinates to ensure they stay within the image boundaries.
            sampleX = Math.max(0, Math.min(width - 1, sampleX));
            sampleY = Math.max(0, Math.min(height - 1, sampleY));

            // Calculate the GIMP_हरी (linear) index for the source and target pixel data arrays.
            // Each pixel consists of 4 bytes (R, G, B, A).
            const sourcePixelIndex = (sampleY * width + sampleX) * 4;
            const targetPixelIndex = (y * width + x) * 4;

            // Copy the pixel data (R, G, B, A) from the randomly sampled source
            // pixel to the current target pixel.
            targetData[targetPixelIndex]     = sourceData[sourcePixelIndex];     // Red
            targetData[targetPixelIndex + 1] = sourceData[sourcePixelIndex + 1]; // Green
            targetData[targetPixelIndex + 2] = sourceData[sourcePixelIndex + 2]; // Blue
            targetData[targetPixelIndex + 3] = sourceData[sourcePixelIndex + 3]; // Alpha
        }
    }

    // Put the processed image data back onto the canvas.
    ctx.putImageData(targetImageData, 0, 0);

    // Return the canvas element with the diffused image.
    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 Image Diffusion Filter Effect Tool allows users to apply a diffusion filter effect to images, creating a softer, blended look by altering pixel data based on a specified strength parameter. This tool is particularly useful for enhancing photographs, creating artistic effects, or preparing images for design projects. Users can adjust the strength of the effect to achieve their desired aesthetic, making it suitable for both casual users and professionals looking to add a creative touch to their visuals.

Leave a Reply

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

Other Image Tools:

Image Push-Processed Film Filter Effect Tool

Image Color Temperature Orange Filter Effect Tool

Image Kodak Ektar 100 Film Filter Effect

Image Yellow Filter Black And White Effect Tool

Image Expired Film Filter Effect Tool

Image Circular Polarizer Filter Effect Tool

Image Lomography Purple Filter Effect Tool

Image Split Field Filter Effect Tool

Image Soft Focus Filter Effect Tool

Image Medium Format Film Filter Effect

Image Wide-Angle Lens Perspective Filter Effect Tool

Olympus OM-1 Photo Filter Effect Tool

Image Fujifilm Velvia Filter Effect Tool

Image Lensbaby Selective Focus Filter Effect Tool

Image Color Temperature Blue Filter Effect Tool

Image UV Filter Effect Tool

Image Red Filter Black And White Effect Tool

Image Redscale Film Filter Effect

Image Cinestill 800T Filter Effect Tool

Image Glimmer Glass Filter Effect Tool

Image Star Filter Effect Tool

Image Kodak Portra 400 Film Filter Effect

Image Fujifilm Superia Filter Effect Tool

Image Tilt-Shift Lens Filter Effect Tool

Image Graduated Neutral Density Filter Effect Tool

Image Diana Camera Filter Effect Tool

Image 35mm Film Camera Filter Effect Tool

Image Pro-Mist Filter Effect Application

Image Cross-Processed Slide Film Filter Effect Application

Image Pull-Processed Film Filter Effect Tool

Image Infrared Filter Effect Application

Image Ilford Delta 3200 Filter Effect Application

Image Cooling Filter Effect Tool

Image Fujifilm Instax Filter Effect Creator

Image Black And White Effect With Orange Filter

Image Kodak Tri-X Filter Effect Tool

See All →