Please bookmark this page to avoid losing your image tool!

Image Minimalist 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, levels = 4, applyGrayscale = "false") {
    const canvas = document.createElement('canvas');
    const ctx = canvas.getContext('2d', { willReadFrequently: true }); // Opt-in for performance with getImageData/putImageData

    // Ensure the image is loaded before trying to get its dimensions
    // This check is more for robustness if the image object might not be fully loaded,
    // though the problem statement implies it's a ready Image object.
    if (!originalImg.complete || originalImg.naturalWidth === 0 || originalImg.naturalHeight === 0) {
        // Fallback or error handling for unloaded/invalid image
        // For this example, we'll create a small placeholder canvas if image is invalid
        // or simply return the empty canvas early.
        // console.error("Image not loaded or invalid.");
        // For a tool, better to draw something or throw error.
        // To keep it simple as per prompt focusing on the filter logic, assuming valid image.
        // If dimensions are 0, it will create a 0x0 canvas.
    }

    canvas.width = originalImg.naturalWidth;
    canvas.height = originalImg.naturalHeight;

    ctx.drawImage(originalImg, 0, 0, canvas.width, canvas.height);

    // Parameter parsing and validation
    let numLevels = typeof levels === 'string' ? parseInt(levels, 10) : Number(levels);
    if (isNaN(numLevels) || numLevels < 2) {
        numLevels = 2; // Minimum 2 levels for posterization (e.g., black and white)
    } else if (numLevels > 256) {
        numLevels = 256; // Max 256 levels (no change for 8-bit color)
    }
    // Ensure numLevels is an integer
    numLevels = Math.floor(numLevels);


    const doGrayscale = String(applyGrayscale).toLowerCase() === "true";

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

    // If numLevels is 1, it's a special case that our formula `255 / (numLevels - 1)` doesn't handle.
    // We interpret 1 level as effectively 2 levels for posterization, or handle as mid-gray if desired.
    // Given the validation `numLevels < 2` sets `numLevels = 2`, this case is covered.
    // The factor for posterization:
    const factor = 255 / (numLevels - 1);

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

        if (doGrayscale) {
            // Standard luminance calculation
            const gray = Math.round(0.299 * r + 0.587 * g + 0.114 * b);
            r = gray;
            g = gray;
            b = gray;
        }

        // Apply posterization to each channel
        data[i] = Math.round(r / factor) * factor;
        data[i + 1] = Math.round(g / factor) * factor;
        data[i + 2] = Math.round(b / factor) * factor;
    }

    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 Minimalist Filter Effect Tool allows users to apply a minimalist effect to images by reducing the number of color levels and optionally converting the image to grayscale. This tool can be used for creating unique artistic images, enhancing photos for modern designs, or simplifying visuals for a cleaner look. It is particularly useful for graphic designers, digital artists, and anyone looking to stylize images for presentations, social media, or personal projects.

Leave a Reply

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