HTML and Markdown conversion fails on a CMYK or truncated image
Bug
With fetch_images=True, a single CMYK image in an HTML or Markdown file makes the whole conversion fail with cannot write mode CMYK as PNG, so none of the document's text comes through. A truncated image fails the same way (Truncated File Read).
ImageResourceLoader.create_image_ref passes the image to ImageRef.from_pil without normalize_image_for_png (the helper #4598 added for Word and PowerPoint), and PNG can't store CMYK. Its except catches UnidentifiedImageError, ValueError and others, but not OSError, which is what Pillow raises here and for a truncated file. So the error ends the conversion instead of becoming a warning like the other image errors.
Steps to reproduce
import base64
from io import BytesIO
from PIL import Image
from docling.datamodel.backend_options import HTMLBackendOptions
from docling.datamodel.base_models import InputFormat
from docling.document_converter import DocumentConverter, HTMLFormatOption
jpeg = BytesIO()
Image.new("CMYK", (200, 100), (0, 255, 255, 0)).save(jpeg, format="JPEG")
src = "data:image/jpeg;base64," + base64.b64encode(jpeg.getvalue()).decode()
with open("cmyk.html", "w") as f:
f.write(f'<p>Some text</p><img src="{src}">')
converter = DocumentConverter(
format_options={
InputFormat.HTML: HTMLFormatOption(
backend_options=HTMLBackendOptions(fetch_images=True)
)
}
)
result = converter.convert("cmyk.html", raises_on_error=False)
print(result.status, [e.error_message for e in result.errors])
# ConversionStatus.FAILURE ['cannot write mode CMYK as PNG']
The same image in RGB converts with SUCCESS. Markdown ( with MarkdownBackendOptions(fetch_images=True)) fails the same way.
Docling version
main at 9590eb4 (the code is the same in 2.135.0)
Python version
Python 3.12.13
I have a fix with tests, PR coming.
Duplicate of #4682