Please bookmark this page to avoid losing your image tool!

Image To Black And White Converter

(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, thresholdParam = 128) {
    let threshold = Number(thresholdParam);

    // Validate the threshold parameter
    // It must be a number between 0 and 255.
    // If thresholdParam is a string, Number(thresholdParam) will convert it.
    // If it's NaN (e.g., Number("abc")) or out of range, use the default 128.
    if (isNaN(threshold) || threshold < 0 || threshold > 255) {
        console.warn(`Invalid threshold value: '${thresholdParam}'. Using default 128.`);
        threshold = 128;
    }

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

    // Determine image dimensions. Use naturalWidth/Height for intrinsic dimensions if available (e.g. <img> elements),
    // otherwise fallback to width/height (e.g. for Image objects created via `new Image()`).
    const imgWidth = originalImg.naturalWidth || originalImg.width;
    const imgHeight = originalImg.naturalHeight || originalImg.height;

    // Handle cases where the image might not be loaded yet or has no dimensions
    if (imgWidth === 0 || imgHeight === 0) {
        console.error("Image has zero dimensions. Ensure it is loaded properly before processing.");
        // Return a small, empty canvas or one with an error message
        canvas.width = 100; // Minimal size to display potential error text
        canvas.height = 30;
        ctx.font = "12px Arial";
        ctx.fillStyle = "red";
        ctx.textAlign = "center";
        ctx.fillText("Error: Image not loaded.", canvas.width / 2, canvas.height / 2 + 4);
        return canvas;
    }

    canvas.width = imgWidth;
    canvas.height = imgHeight;

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

    let imageData;
    try {
        // Get the pixel data from the canvas
        imageData = ctx.getImageData(0, 0, imgWidth, imgHeight);
    } catch (e) {
        // This can happen if the image is cross-origin and the canvas becomes tainted
        console.error("Could not get ImageData from canvas. Image might be cross-origin and canvas is tainted.", e);
        // Clear canvas and draw an error message
        ctx.clearRect(0,0,canvas.width, canvas.height); // Clear the drawn image
        ctx.font = "16px Arial";
        ctx.fillStyle = "red";
        ctx.textAlign = "center";
        // Break message into multiple lines if canvas is too small horizontally
        const message = "Error: Cannot process cross-origin image.";
        if (canvas.width < ctx.measureText(message).width) {
             ctx.fillText("Error: Cross-origin", canvas.width / 2, canvas.height / 2 - 10);
             ctx.fillText("image issue.", canvas.width / 2, canvas.height / 2 + 10);
        } else {
            ctx.fillText(message, canvas.width / 2, canvas.height / 2);
        }
        return canvas;
    }
    
    const data = imageData.data; // This is a Uint8ClampedArray: [R,G,B,A, R,G,B,A, ...]

    // Iterate over each pixel (4 bytes at a time: R, G, B, A)
    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 pixel to grayscale using the luminosity method (standard for perceived brightness)
        // Formula: 0.299*R + 0.587*G + 0.114*B
        const gray = 0.299 * r + 0.587 * g + 0.114 * b;

        // Apply the threshold: if grayscale value is above threshold, set to white (255), else black (0)
        const value = gray >= threshold ? 255 : 0;

        data[i] = value;     // Red channel
        data[i + 1] = value; // Green channel
        data[i + 2] = value; // Blue channel
        // data[i + 3] (alpha) is left unchanged
    }

    // Put the modified image data back onto the canvas
    ctx.putImageData(imageData, 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 Image To Black And White Converter tool allows you to convert colorful images into black and white (grayscale) representations. Users can specify a threshold value to determine how light or dark pixels will be treated, enhancing the effect of the conversion. This tool is useful for creating artistic representations, analyzing images without color distractions, or when black and white images are preferable for printing or historical purposes. It effectively supports various image formats and helps in achieving clean and impactful visuals.

Leave a Reply

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

Other Image Tools:

Image Motion Blur Applicator

Image Glow Effect Applicator

Image Retro Effect Applicator

Image Film Effect Creator

Image Wanted Poster Generator

Image To ASCII Art Converter

Image To The Pink Panther Show Episode Details Finder

Image To Pink Panther Show Episode Details Finder

Image To Big Hero 6 The Series 2017-2021 Show Details Finder

Image To Illumination Entertainment Movie Details Finder

Image To Walt Disney Animation Studios Movie Details Finder

Image To THX Logo History 1983-2023 Search Tool

Image To Thx 1983-2023 Search Tool

20th Century Fox 1935-2020 Variants Image Viewer

Image To Detailed Audio Description Generator

Detailed Dreamworks Image Variant Generator

Image To The Pink Panther Show Episode 1-124 Search Tool

Image To Tubi September 2026 Coming Soon Movie Search Tool

Image To Tubi Sep 2026 Movie Search Tool

Image To Dottie Chicken Movie Search Tool

Image To Gahlina Pintadinha Movie Search Tool

Image and Video Big Hero 6 The Series Search Tool

Image To Illumination Entertainment Movie Search Tool

Image Soft Bubble Overlay Adder

Image Aspect Ratio 20:9 Stretch Tool

Image To Music Search Finder

Image To Movie Details Finder

Image To Walt Disney Animation Studios Movie Search Tool

Beauty and the Beast Special Extended Edition 1991 Movie Info Tool

Image Information Extractor for Movie Details

Stretch Image To 9:16 Aspect Ratio

Image Aspect Ratio 2.55:1 Resizer

Image To 20:9 Aspect Ratio Converter

Big Hero 6 2014 Wonder Project Amazon Channel Video Image Tool

Android Ringtone MP3 Photo Player

See All →