diff --git a/README.md b/README.md index 00609a0..811865a 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,8 @@ chapitre contient des images (désactivable avec `--no-ocr` / `"with_ocr": false l'OCR extrait les régions texte des images (`ocr.json`, avec le quadrilatère `poly` quand le moteur le fournit — PaddleOCR), chaque texte détecté est traduit par le LLM, puis `typeset` efface le texte original (**inpainting**, voir moteurs ci-dessous) et redessine la traduction au même endroit, -centrée, avec une couleur contrastée avec le fond (Pillow). Résultat : `export/images-/`. +centrée, à la taille du texte d'origine (hauteur de la boîte OCR), avec une couleur contrastée +avec le fond (Pillow). Résultat : `export/images-/`. La langue source de l'OCR est déduite de la langue détectée du chapitre (`en→eng`, `fr→fra`, `ja→jpn`…). L'image Docker n'embarque que `eng` (`tesseract-ocr` inclut `tesseract-ocr-eng`) : diff --git a/src/scalper/typeset.py b/src/scalper/typeset.py index 96257c1..dd14bc1 100644 --- a/src/scalper/typeset.py +++ b/src/scalper/typeset.py @@ -40,6 +40,24 @@ def wrap_text(draw, text: str, font, max_width: int) -> list[str]: return lines or [""] +def _fit_font(draw, text: str, max_w: int, max_h: int) -> tuple[Any, int, list[str]]: + """Plus grande police (>= 8) tenant dans max_w x max_h : renvoie (font, taille, lignes). + On part de la hauteur max (la boîte OCR ≈ la hauteur du texte d'origine), puis on + réduit jusqu'à tenir en largeur ET en hauteur.""" + font_size = max(8, max_h - 2) + font = _load_font(font_size) + lines = wrap_text(draw, text, font, max(10, max_w - 8)) + while font_size > 8: + too_tall = (font_size + 2) * len(lines) > max_h + too_wide = max(draw.textlength(ln, font=font) for ln in lines) > max_w - 8 + if not too_tall and not too_wide: + break + font_size -= 1 + font = _load_font(font_size) + lines = wrap_text(draw, text, font, max(10, max_w - 8)) + return font, font_size, lines + + def _text_colors(fill_rgb: tuple[int, int, int]) -> tuple[tuple[int, int, int], tuple[int, int, int]]: """Couleur de texte + contour contrastées avec le fond (luminance).""" lum = 0.299 * fill_rgb[0] + 0.587 * fill_rgb[1] + 0.114 * fill_rgb[2] @@ -89,19 +107,8 @@ def replace_texts( x, y, w, h = r["box"] fill_rgb = estimate_bg_color(img, [x, y, w, h]) text_fill, stroke_fill = _text_colors(fill_rgb) - # taille de départ proche du texte original (hauteur de boîte ≈ hauteur de ligne) - font_size = max(8, int(h * 0.8)) - font = _load_font(font_size) - lines = wrap_text(draw, text, font, max(10, w - 8)) - # on réduit jusqu'à tenir dans la boîte, en largeur ET en hauteur - while font_size > 8: - too_tall = (font_size + 2) * len(lines) > h - too_wide = max(draw.textlength(ln, font=font) for ln in lines) > w - 8 - if not too_tall and not too_wide: - break - font_size -= 1 - font = _load_font(font_size) - lines = wrap_text(draw, text, font, max(10, w - 8)) + # même taille que le texte d'origine : on part de la hauteur de la boîte OCR + font, font_size, lines = _fit_font(draw, text, w, h) line_h = font_size + 2 ty = y + max(0, (h - line_h * len(lines)) // 2) for ln in lines: diff --git a/tests/test_ocr_typeset.py b/tests/test_ocr_typeset.py index 3283b39..a280a39 100644 --- a/tests/test_ocr_typeset.py +++ b/tests/test_ocr_typeset.py @@ -191,6 +191,17 @@ class TestOCRTypeset(unittest.TestCase): colors = img.getcolors(maxcolors=100000) self.assertGreater(len(colors), 2, "le texte redessiné doit modifier des pixels") + def test_fit_font_uses_full_original_box_height(self): + img = Image.new("RGB", (10, 10)) + d = ImageDraw.Draw(img) + from scalper.typeset import _fit_font + font, size, lines = _fit_font(d, "Bonjour", 380, 40) + self.assertEqual(size, 38, "taille max = hauteur de boîte OCR - 2 (même taille que l'original)") + self.assertEqual(lines, ["Bonjour"]) + _, size2, lines2 = _fit_font(d, "Une traduction beaucoup plus longue que l'original", 120, 30) + self.assertLess(size2, 28) + self.assertGreater(len(lines2), 1) + def test_wrap_text(self): img = Image.new("RGB", (10, 10)) d = ImageDraw.Draw(img)