Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docling/backend/latex/handlers/macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from docling.backend.latex.utils.encoding import decode_latex_content
from docling.backend.latex.utils.latex_context import LATEX_CONTEXT_DB
from docling.backend.latex.utils.text import iter_child_nodelists
from docling.backend.utils.image import normalize_image_for_png

if TYPE_CHECKING:
from typing import Any
Expand Down Expand Up @@ -374,7 +375,9 @@ def _process_macro( # noqa: C901
_log.debug(
f"Loaded image {img_path}: {pil_image.size}, DPI={dpi}"
)
image = ImageRef.from_pil(image=pil_image, dpi=int(dpi))
image = ImageRef.from_pil(
image=normalize_image_for_png(pil_image), dpi=int(dpi)
)
except Exception as e:
_log.debug(f"Could not load image {img_path}: {e}")

Expand Down
26 changes: 26 additions & 0 deletions tests/test_latex/test_figures.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,32 @@ def test_latex_includegraphics():
assert "test_image.png" in picture.captions[0].resolve(doc).text


def test_latex_includegraphics_cmyk_jpeg(tmp_path: Path):
"""A CMYK JPEG figure keeps its image; PNG cannot store CMYK."""
from PIL import Image as PILImage

PILImage.new("CMYK", (100, 50), (0, 255, 255, 0)).save(tmp_path / "fig.jpg")
tex_file = tmp_path / "test.tex"
tex_file.write_bytes(
b"\\documentclass{article}\n\\begin{document}\n"
b"\\includegraphics{fig.jpg}\n\\end{document}\n"
)

in_doc = InputDocument(
path_or_stream=tex_file,
format=InputFormat.LATEX,
backend=LatexDocumentBackend,
filename="test.tex",
)
doc = LatexDocumentBackend(in_doc=in_doc, path_or_stream=tex_file).convert()

assert len(doc.pictures) == 1
image = doc.pictures[0].get_image(doc)
assert image is not None
assert image.mode == "RGB"
assert image.getpixel((50, 25)) == (255, 0, 0)


def test_latex_includegraphics_missing_image():
"""Test includegraphics gracefully handles missing images"""
latex_content = b"""
Expand Down
Loading