Please bookmark this page to avoid losing your image tool!

Image Text Alphabet Translator

(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.
async function processImage(originalImg, alphabet = 'ΑВCЅΕFGНIЈКLМNОРQЯЅТՍVWΧΥΖ') {
    /**
     * Dynamically loads the Tesseract.js script from a CDN.
     * @returns {Promise<void>} A promise that resolves when the script is loaded.
     */
    const loadTesseract = () => {
        return new Promise((resolve, reject) => {
            if (window.Tesseract) {
                return resolve();
            }
            const script = document.createElement('script');
            script.src = 'https://cdn.jsdelivr.net/npm/tesseract.js@5/dist/tesseract.min.js';
            script.onload = resolve;
            script.onerror = () => reject(new Error('Failed to load Tesseract.js library.'));
            document.head.appendChild(script);
        });
    };

    try {
        await loadTesseract();
    } catch (error) {
        console.error(error);
        const errorCanvas = document.createElement('canvas');
        errorCanvas.width = originalImg.naturalWidth > 0 ? originalImg.naturalWidth : 400;
        errorCanvas.height = originalImg.naturalHeight > 0 ? originalImg.naturalHeight : 100;
        const errorCtx = errorCanvas.getContext('2d');
        errorCtx.fillStyle = '#f0f0f0';
        errorCtx.fillRect(0, 0, errorCanvas.width, errorCanvas.height);
        errorCtx.fillStyle = 'red';
        errorCtx.font = '16px Arial';
        errorCtx.textAlign = 'center';
        errorCtx.textBaseline = 'middle';
        errorCtx.fillText('Error: Could not load text recognition library.', errorCanvas.width / 2, errorCanvas.height / 2);
        return errorCanvas;
    }

    // Setup the main canvas to draw on
    const canvas = document.createElement('canvas');
    // Use the { willReadFrequently: true } hint for performance optimization
    const ctx = canvas.getContext('2d', { willReadFrequently: true });
    canvas.width = originalImg.naturalWidth;
    canvas.height = originalImg.naturalHeight;
    ctx.drawImage(originalImg, 0, 0, canvas.width, canvas.height);

    // Create a character translation map from the Latin alphabet to the custom alphabet
    const map = {};
    const latin = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    if (!alphabet || alphabet.length === 0) {
        alphabet = '?'; // Fallback for an empty alphabet string
    }
    for (let i = 0; i < 26; i++) {
        // Map both uppercase and lowercase Latin characters to the provided alphabet
        map[latin[i]] = alphabet[i % alphabet.length];
        map[latin[i + 26]] = alphabet[i % alphabet.length];
    }

    // Initialize Tesseract worker to recognize English text
    const worker = await Tesseract.createWorker('eng', 1, {
        logger: m => console.log(m) // Logs progress to the developer console
    });

    // Recognize text from the canvas
    const { data: { words } } = await worker.recognize(canvas);
    await worker.terminate();

    // Process each recognized word
    for (const word of words) {
        // Skip words with low confidence to avoid errors
        if (word.confidence < 60) continue;

        const bbox = word.bbox;
        const w = bbox.x1 - bbox.x0;
        const h = bbox.y1 - bbox.y0;

        if (w <= 0 || h <= 0) continue;

        // Erase the original text by applying a blur filter to its bounding box
        ctx.save();
        ctx.beginPath();
        ctx.rect(bbox.x0, bbox.y0, w, h);
        ctx.clip(); // Restrict the blur effect to the bounding box
        ctx.filter = `blur(${Math.min(w, h) * 0.15}px)`; // Blur radius is proportional to text size
        // Draw the image area back onto itself with the blur filter applied
        ctx.drawImage(canvas, bbox.x0, bbox.y0, w, h, bbox.x0, bbox.y0, w, h);
        ctx.restore(); // Remove the clip and filter settings

        // Translate the recognized word using the character map
        const translatedText = word.text.split('').map(char => map[char] || char).join('');

        // Set font properties for drawing the new text
        // Tesseract v5 often provides the original text color, which we can reuse
        ctx.fillStyle = word.color || '#000000'; 
        const fontSize = h * 0.85; // Estimate font size from bounding box height
        ctx.font = `bold ${fontSize}px Arial, sans-serif`;
        ctx.textAlign = 'left';
        ctx.textBaseline = 'middle';

        // Measure the new text and calculate a scale factor to make it fit
        const textWidth = ctx.measureText(translatedText).width;
        if (textWidth <= 0) continue;
        
        // Use 95% of the original width to provide some padding
        const scaleFactor = (w * 0.95) / textWidth;

        // Draw the new, translated text with horizontal scaling
        ctx.save();
        // Position the drawing context at the start and vertical middle of the word
        ctx.translate(bbox.x0 + (w * 0.025), bbox.y0 + h / 2);
        ctx.scale(scaleFactor, 1);
        ctx.fillText(translatedText, 0, 0);
        ctx.restore();
    }

    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 Text Alphabet Translator is a tool that converts text found in images to a specified alphabet representation. It utilizes optical character recognition (OCR) to detect and extract text from images and then translates this text into a user-defined alphabet mapping. This tool can be useful for creating stylized text interpretations from photographs, enhancing images for artistic purposes, or even converting text for different writing systems in educational contexts. Whether you need to adapt images for graphic design, digital media, or language learning, this tool provides a straightforward solution for transforming textual content visually.

Leave a Reply

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