Dev.to AI 🤖 Ai 👁 0 📖 3 min read

How I fixed misspelled text in AI-generated images (Node.js + sharp)

If you have ever asked an image model to write "7 Days in Paradise" on a thumbnail, you know the result: 7 Dyas in Paradlse. Image models draw letters as shapes, not as words. Short common words sometimes survive; names

If you have ever asked an image model to write "7 Days in Paradise" on a thumbnail, you know the result: 7 Dyas in Paradlse. Image models draw letters as shapes, not as words. Short common words sometimes survive; names and titles usually don't.

I run a free text-to-image site, Dreamixel, and several of its tools need exact text: name posters, YouTube thumbnails, invitations, greeting cards. Here is the approach that finally made the text reliable — it is boring, which is why it works.

The idea: never let the model write

  1. Ask the model for the artwork only, and explicitly ask for an empty, calmer area where text will go ("leave a clear darker area in the center, do NOT add any text").
  2. Render the user's text yourself with a real font.
  3. Composite it on top on the server.

The model is good at mood, lighting and composition. Typography is a solved problem. Let each side do what it is good at.

Rendering text with sharp

sharp can render text through Pango, which gives you word wrapping, alignment and a real font file:

import sharp from "sharp";

async function renderText(text, color, width, height) {
  const buf = await sharp({
    text: {
      text: `<span foreground="${color}">${escapeXml(text)}</span>`,
      fontfile: "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
      font: "DejaVu Sans Bold",
      width,          // max box width in px
      height,         // max box height in px — sharp picks the font size to fit
      align: "centre",
      wrap: "word",
      rgba: true,
    },
  }).png().toBuffer();
  const { width: w, height: h } = await sharp(buf).metadata();
  return { buf, w, h };
}

const escapeXml = (s) =>
  s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");

Passing both width and height lets sharp choose the largest font size that fits the box, so short names get huge and long titles wrap nicely without any measuring code on my side.

Escape the text: Pango markup is XML-ish, and users will type & and <.

Making it readable on any background

White text on a random AI background is a gamble. Two cheap tricks make it readable almost always:

async function bannerOverlay(input, text) {
  const { width: W, height: H } = await sharp(input).metadata();
  const boxW = Math.round(W * 0.78), boxH = Math.round(H * 0.3);

  const t  = await renderText(text, "#ffffff", boxW, boxH);
  const sh = await renderText(text, "#000000", boxW, boxH);
  const shadow = await sharp(sh.buf).blur(Math.max(2, Math.round(H / 160))).png().toBuffer();

  // 1) a soft radial darkening behind the text
  const glow = Buffer.from(
    `<svg width="${W}" height="${H}"><defs><radialGradient id="g">` +
    `<stop offset="0%" stop-color="#000" stop-opacity="0.55"/>` +
    `<stop offset="100%" stop-color="#000" stop-opacity="0"/></radialGradient></defs>` +
    `<ellipse cx="${W/2}" cy="${H/2}" rx="${W*0.48}" ry="${H*0.34}" fill="url(#g)"/></svg>`
  );

  const left = Math.round((W - t.w) / 2), top = Math.round((H - t.h) / 2);
  const off = Math.max(2, Math.round(H / 220));

  return sharp(input).composite([
    { input: glow, left: 0, top: 0 },
    { input: shadow, left: left + off, top: top + off }, // 2) blurred drop shadow
    { input: t.buf, left, top },
  ]).png().toBuffer();
}
  1. A radial darkening behind the text area — invisible on dark images, saves you on bright ones.
  2. A blurred black copy of the text as a drop shadow, offset by a few pixels.

Scaling the blur and offset with image height keeps it consistent between a 1024px square and a 1920px portrait.

Prompting for space

The overlay only looks good if the artwork leaves room. Each tool on the site prepends something like this to the user's prompt:

"...with a clear, calm, slightly darker area in the center for a headline (do NOT add any text, letters or numbers). Theme: {user prompt}"

Saying "do NOT add any text" explicitly matters; otherwise models love to add fake lettering that then collides with yours.

Results

The same pipeline now powers name posters, YouTube thumbnails, invitations and classroom posters. Spelling errors went from "most of the time" to zero, because the model never writes.

Limitations

  • One font for everything looks fine for headlines but generic for, say, wedding invitations. Next step is per-tool fonts.
  • Scripts that need shaping (Arabic, Devanagari) depend on the fonts installed on the server — test them, don't assume.
  • It's a single centered block. Multi-line layouts (title + date + address) need a small layout engine.

If you're building anything that puts user text on generated images, I'd strongly recommend this split. Happy to answer questions in the comments.

Disclosure: I'm the maker of Dreamixel.

📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.