Please bookmark this page to avoid losing your image tool!

Image To Semiperfect Number Representation 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, resolution = "80", colorMode = "original", monochromeColor = "#FFFFFF", backgroundColor = "#000000") {
    // Helper to generate the first 256 semiperfect numbers.
    // A semiperfect (or pseudoperfect) number is equal to the sum of all or some of its proper divisors.
    const semiperfectNumbers = [];
    let n = 2;
    while(semiperfectNumbers.length < 256) {
        let divisors = [];
        let sum = 0;
        
        // Find proper divisors
        for(let i = 1; i <= Math.floor(n / 2); i++) {
            if(n % i === 0) {
                divisors.push(i);
                sum += i;
            }
        }
        
        // A semiperfect number must be abundant or perfect
        if(sum >= n) {
            // Use dynamic programming subset-sum to check if any subset of divisors sums to 'n'
            let dp = new Uint8Array(n + 1);
            dp[0] = 1;
            for (let i = 0; i < divisors.length; i++) {
                let d = divisors[i];
                for (let j = n; j >= d; j--) {
                    if (dp[j - d]) {
                        dp[j] = 1;
                    }
                }
            }
            if (dp[n]) {
                semiperfectNumbers.push(n);
            }
        }
        n++;
    }

    // Determine grid size
    const cols = Math.max(1, parseInt(resolution, 10));
    const cellWidth = originalImg.width / cols;
    const cellHeight = cellWidth; // Keep cells square mathematically
    const rows = Math.max(1, Math.floor(originalImg.height / cellHeight));

    // Draw the original image to a small offscreen canvas to sample average cell colors
    const sampleCanvas = document.createElement('canvas');
    sampleCanvas.width = cols;
    sampleCanvas.height = rows;
    const sCtx = sampleCanvas.getContext('2d', { willReadFrequently: true });
    sCtx.drawImage(originalImg, 0, 0, cols, rows);
    const imgData = sCtx.getImageData(0, 0, cols, rows).data;

    // Create the final output canvas
    const canvas = document.createElement('canvas');
    canvas.width = originalImg.width;
    canvas.height = rows * cellHeight; // Adjust height slightly to fit full rows perfectly
    const ctx = canvas.getContext('2d');

    // Fill background
    ctx.fillStyle = backgroundColor;
    ctx.fillRect(0, 0, canvas.width, canvas.height);

    // Context text styling
    ctx.textBaseline = 'middle';
    ctx.textAlign = 'center';
    
    // Scale font size down slightly so characters fit nicely
    const fontSize = cellHeight * 0.9;
    ctx.font = `bold ${fontSize}px monospace`;

    // Process and draw the semiperfect text based on brightness
    for (let y = 0; y < rows; y++) {
        for (let x = 0; x < cols; x++) {
            const i = (y * cols + x) * 4;
            const r = imgData[i];
            const g = imgData[i + 1];
            const b = imgData[i + 2];
            const a = imgData[i + 3];

            if (a === 0) continue; // Skip fully transparent pixels

            // Perceptive luminance / brightness formula
            const brightness = Math.floor(0.299 * r + 0.587 * g + 0.114 * b);
            
            // Map brightness (0-255) to the generated array of semiperfect numbers
            const number = semiperfectNumbers[brightness];

            // Resolve color based on colorMode parameter
            let fillColor = "";
            if (colorMode === "grayscale") {
                fillColor = `rgb(${brightness}, ${brightness}, ${brightness})`;
            } else if (colorMode === "monochrome") {
                fillColor = monochromeColor;
            } else { // "original" mode
                fillColor = `rgba(${r}, ${g}, ${b}, ${a / 255})`;
            }

            ctx.fillStyle = fillColor;

            // Positioning in the center of the current grid cell
            const drawX = x * cellWidth + cellWidth / 2;
            const drawY = y * cellHeight + cellHeight / 2;

            // Draw text, squishing it horizontally if it's too wide for the cell
            ctx.fillText(number.toString(), drawX, drawY, cellWidth * 0.95);
        }
    }

    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

This tool converts images into a unique visual representation composed of semiperfect numbers. By analyzing the brightness of each pixel, the converter maps color values to a specific sequence of semiperfect numbers (numbers that are equal to the sum of some or all of their proper divisors) and displays them in a grid format. Users can customize the output with different color modes, including original color, grayscale, or monochrome, and adjust the resolution. This tool is ideal for creating artistic, mathematically-themed digital art, unique textures, or conceptual visual content for creative projects.

Leave a Reply

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