Skip to content

Commit 7f83f1c

Browse files
feat: add image group serialization in html (#284)
* feat: add-image group serialization in html Signed-off-by: Peter Staar <[email protected]> * reformatted the code Signed-off-by: Peter Staar <[email protected]> --------- Signed-off-by: Peter Staar <[email protected]>
1 parent 511fb98 commit 7f83f1c

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

docling_core/transforms/serializer/html.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,13 @@ def serialize(
370370
**kwargs: Any,
371371
) -> SerializationResult:
372372
"""Export picture to HTML format."""
373+
374+
def get_img_row(imgb64: str, ind: int) -> str:
375+
row = '<tr><td style="border: 2px solid black; padding: 8px;">'
376+
row += f'<img src="data:image/png;base64,{imgb64}" alt="image {ind}">'
377+
row += "</td></tr>\n"
378+
return row
379+
373380
params = HTMLParams(**kwargs)
374381

375382
res_parts: list[SerializationResult] = []
@@ -393,13 +400,32 @@ def serialize(
393400
and item.image.uri.scheme == "data"
394401
):
395402
img_text = f'<img src="{item.image.uri}">'
403+
elif len(item.prov) > 1: # more than 1 provenance
404+
405+
img_text = (
406+
'<table style="border-collapse: collapse; width: 100%;">\n'
407+
)
408+
for ind, prov in enumerate(item.prov):
409+
img = item.get_image(doc, prov_index=ind)
410+
411+
if img is not None:
412+
imgb64 = item._image_to_base64(img)
413+
img_text += get_img_row(imgb64=imgb64, ind=ind)
414+
else:
415+
_logger.warning("Could not get image")
416+
417+
img_text += "</table>\n"
418+
396419
else:
397420
# get the item.image._pil or crop it out of the page-image
398421
img = item.get_image(doc)
399422

400423
if img is not None:
401424
imgb64 = item._image_to_base64(img)
402425
img_text = f'<img src="data:image/png;base64,{imgb64}">'
426+
else:
427+
_logger.warning("Could not get image")
428+
403429
elif params.image_mode == ImageRefMode.REFERENCED:
404430
if isinstance(item.image, ImageRef) and not (
405431
isinstance(item.image.uri, AnyUrl)

docling_core/types/doc/document.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,9 @@ def get_location_tokens(
790790

791791
return location
792792

793-
def get_image(self, doc: "DoclingDocument") -> Optional[PILImage.Image]:
793+
def get_image(
794+
self, doc: "DoclingDocument", prov_index: int = 0
795+
) -> Optional[PILImage.Image]:
794796
"""Returns the image of this DocItem.
795797
796798
The function returns None if this DocItem has no valid provenance or
@@ -800,15 +802,15 @@ def get_image(self, doc: "DoclingDocument") -> Optional[PILImage.Image]:
800802
if not len(self.prov):
801803
return None
802804

803-
page = doc.pages.get(self.prov[0].page_no)
805+
page = doc.pages.get(self.prov[prov_index].page_no)
804806
if page is None or page.size is None or page.image is None:
805807
return None
806808

807809
page_image = page.image.pil_image
808810
if not page_image:
809811
return None
810812
crop_bbox = (
811-
self.prov[0]
813+
self.prov[prov_index]
812814
.bbox.to_top_left_origin(page_height=page.size.height)
813815
.scale_to_size(old_size=page.size, new_size=page.image.size)
814816
# .scaled(scale=page_image.height / page.size.height)
@@ -973,7 +975,9 @@ def caption_text(self, doc: "DoclingDocument") -> str:
973975
text += cap.resolve(doc).text
974976
return text
975977

976-
def get_image(self, doc: "DoclingDocument") -> Optional[PILImage.Image]:
978+
def get_image(
979+
self, doc: "DoclingDocument", prov_index: int = 0
980+
) -> Optional[PILImage.Image]:
977981
"""Returns the image corresponding to this FloatingItem.
978982
979983
This function returns the PIL image from self.image if one is available.
@@ -985,7 +989,7 @@ def get_image(self, doc: "DoclingDocument") -> Optional[PILImage.Image]:
985989
"""
986990
if self.image is not None:
987991
return self.image.pil_image
988-
return super().get_image(doc=doc)
992+
return super().get_image(doc=doc, prov_index=prov_index)
989993

990994

991995
class CodeItem(FloatingItem, TextItem):

0 commit comments

Comments
 (0)