Please bookmark this page to avoid losing your image tool!

Image CT Scan 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, windowLevelParam = 128, windowWidthParam = 128) {
    // Parameter handling: Convert to numbers and ensure sane values.
    // Default values from function signature are used if params are not provided.
    let level = Number(windowLevelParam);
    if (Number.isNaN(level)) {
        // If conversion results in NaN (e.g., input was "abc"), revert to a default.
        // Using the default specified in the function signature for consistency.
        level = 128; 
    }

    let width = Number(windowWidthParam);
    if (Number.isNaN(width)) {
        // If conversion results in NaN, revert to a default.
        width = 128;
    }
    
    // Ensure width is at least 1.0 to prevent division by zero or negative width issues.
    // A width smaller than 1 (e.g. 0.5) will be treated as 1. 
    // A negative or zero width from user input will also be corrected to 1.
    width = Math.max(1, width);

    const canvas = document.createElement('canvas');
    const ctx = canvas.getContext('2d');

    // Set canvas dimensions to the image's natural dimensions
    canvas.width = originalImg.naturalWidth || originalImg.width;
    canvas.height = originalImg.naturalHeight || originalImg.height;

    // Draw the original image onto the canvas
    ctx.drawImage(originalImg, 0, 0, canvas.width, canvas.height);

    try {
        const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
        const data = imageData.data;

        // Calculate the range of intensities that will be visible
        const minVisibleIntensity = level - width / 2;
        const maxVisibleIntensity = level + width / 2;

        for (let i = 0; i < data.length; i += 4) {
            const r = data[i];
            const g = data[i + 1];
            const b = data[i + 2];
            // Alpha (data[i + 3]) is preserved

            // Convert to grayscale using the luminosity method (standard for perceived brightness)
            const gray = 0.299 * r + 0.587 * g + 0.114 * b;

            let outputGray;

            if (gray <= minVisibleIntensity) {
                outputGray = 0; // Intensities below the window are black
            } else if (gray >= maxVisibleIntensity) {
                outputGray = 255; // Intensities above the window are white
            } else {
                // Linearly scale the intensity within the window to the full 0-255 range
                // This check for 'width === 0' is technically redundant due to 'width = Math.max(1, width)'
                // but kept for logical clarity if that line were to be changed.
                if (width === 0) { 
                    outputGray = (gray >= level) ? 255 : 0; // Threshold if width is zero
                } else {
                    outputGray = ((gray - minVisibleIntensity) / width) * 255;
                }
            }
            
            // Ensure the value is an integer and clamped to the 0-255 range
            outputGray = Math.round(outputGray);
            outputGray = Math.max(0, Math.min(255, outputGray));

            data[i] = outputGray;     // Set Red channel
            data[i + 1] = outputGray; // Set Green channel
            data[i + 2] = outputGray; // Set Blue channel
        }

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

    } catch (e) {
        // Handle potential errors, e.g., security errors with cross-origin images
        console.error("Error processing image for CT Scan effect:", e);
        
        // Fallback: Clear canvas (if partially drawn) and draw the original image with an error message.
        ctx.clearRect(0, 0, canvas.width, canvas.height); 
        ctx.drawImage(originalImg, 0, 0, canvas.width, canvas.height); 
        
        ctx.fillStyle = "rgba(255, 0, 0, 0.7)"; // Semi-transparent red for overlay
        ctx.font = "bold 16px Arial";
        ctx.textAlign = "center";
        ctx.textBaseline = "middle";
        ctx.fillText("Error applying CT Scan effect.", canvas.width / 2, canvas.height / 2);
    }

    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 CT Scan Filter Effect Tool allows users to apply a CT scan-like filter effect to images by manipulating their intensity levels and converting them to grayscale. This tool can be beneficial for medical professionals, students, or artists in visualizing cross-sectional images, enhancing the contrast in images for analysis, or creating artistic representations that mimic medical imaging styles. Users can customize the window level and width parameters to adjust the visibility of certain intensity ranges in the image, helping in highlighting specific features or structures.

Leave a Reply

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