"""Tests del crop horizontal uniforme (referencia: página del medio).""" from pathlib import Path from PIL import Image, ImageDraw from valeria.renderer import ( _aplicar_crop_horizontal, _detectar_margenes_horizontales, ) def _png_con_marco_blanco( path: Path, total_w: int, total_h: int, content_box: tuple[int, int, int, int], fondo=(255, 255, 255), contenido=(200, 50, 50), ) -> None: im = Image.new("RGB", (total_w, total_h), fondo) ImageDraw.Draw(im).rectangle(content_box, fill=contenido) im.save(str(path), "PNG") # ── _detectar_margenes_horizontales ──────────────────────────────────────── def test_detecta_left_right_de_contenido(tmp_path: Path): p = tmp_path / "im.png" # Contenido pintado entre col 120 y col 999 (inclusive) → bbox (120, ?, 1000, ?) _png_con_marco_blanco(p, 1200, 800, (120, 100, 999, 700)) left, right = _detectar_margenes_horizontales(p) assert left == 120 assert right == 1000 # exclusivo def test_imagen_toda_blanca_devuelve_ancho_completo(tmp_path: Path): p = tmp_path / "blanca.png" Image.new("RGB", (500, 400), (255, 255, 255)).save(str(p), "PNG") left, right = _detectar_margenes_horizontales(p) assert (left, right) == (0, 500) def test_tolerancia_15_trata_grises_claros_como_blanco(tmp_path: Path): """Pixeles 240 (≥ 255-15) cuentan como blanco; el contenido oscuro en columna 200 marca el inicio.""" p = tmp_path / "gris.png" im = Image.new("RGB", (600, 400), (240, 240, 240)) ImageDraw.Draw(im).rectangle((200, 100, 400, 300), fill=(20, 20, 20)) im.save(str(p), "PNG") left, right = _detectar_margenes_horizontales(p, tolerancia_grises=15) assert left == 200 assert right == 401 # 400 inclusive → 401 exclusive # ── _aplicar_crop_horizontal ─────────────────────────────────────────────── def test_aplica_crop_horizontal_recorta(tmp_path: Path): p = tmp_path / "x.png" _png_con_marco_blanco(p, 1000, 600, (200, 0, 799, 599)) aplico = _aplicar_crop_horizontal(p, 200, 800) assert aplico is True with Image.open(str(p)) as im: assert im.size == (600, 600) def test_aplica_crop_horizontal_no_toca_si_cubre_todo(tmp_path: Path): p = tmp_path / "x.png" _png_con_marco_blanco(p, 500, 500, (0, 0, 499, 499)) aplico = _aplicar_crop_horizontal(p, 0, 500) assert aplico is False with Image.open(str(p)) as im: assert im.size == (500, 500) def test_aplica_crop_horizontal_preserva_alto(tmp_path: Path): """No hay recorte vertical: el alto del PNG queda intacto.""" p = tmp_path / "x.png" _png_con_marco_blanco(p, 1000, 1500, (100, 200, 900, 1300)) _aplicar_crop_horizontal(p, 100, 900) with Image.open(str(p)) as im: w, h = im.size assert w == 800 assert h == 1500 # alto sin cambios def test_crop_uniforme_aplica_mismo_recorte_a_varias_paginas(tmp_path: Path): """Simula el flujo: detectar márgenes en página del medio, aplicar a TODAS las páginas (incluso si su contenido era más estrecho).""" pages = [] # Página 0: contenido col 50–949 (más ancho) p0 = tmp_path / "p0.png" _png_con_marco_blanco(p0, 1000, 800, (50, 100, 949, 700)) pages.append(p0) # Página 1 (la del medio): contenido col 150–849 p1 = tmp_path / "p1.png" _png_con_marco_blanco(p1, 1000, 800, (150, 100, 849, 700)) pages.append(p1) # Página 2: contenido col 300–699 (más estrecho) p2 = tmp_path / "p2.png" _png_con_marco_blanco(p2, 1000, 800, (300, 100, 699, 700)) pages.append(p2) # Detectar margins en la del medio medio = pages[len(pages) // 2] left, right = _detectar_margenes_horizontales(medio) assert left == 150 assert right == 850 # Aplicar a todas for p in pages: _aplicar_crop_horizontal(p, left, right) # Todas quedan con el mismo ancho (right - left = 700) y alto 800 for p in pages: with Image.open(str(p)) as im: assert im.size == (700, 800)