Skip to content
Open
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
28 changes: 25 additions & 3 deletions docling_core/types/doc/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
)
from urllib.parse import unquote

import numpy as np
import pandas as pd
import yaml
from PIL import Image as PILImage
Expand Down Expand Up @@ -67,6 +68,13 @@
from docling_core.types.doc.tokens import DocumentToken, TableToken
from docling_core.types.doc.utils import parse_otsl_table_content, relative_path

try:
import cv2

CV2_INSTALLED = True
except ImportError:
CV2_INSTALLED = False

_logger = logging.getLogger(__name__)

Uint64 = typing.Annotated[int, Field(ge=0, le=(2**64 - 1))]
Expand Down Expand Up @@ -1107,12 +1115,26 @@ def validate_mimetype(cls, v):
raise ValueError(f"'{v}' is not a valid MIME type")
return v

@classmethod
def from_pil(cls, image: PILImage.Image, dpi: int) -> Self:
"""Construct ImageRef from a PIL Image."""
@staticmethod
def _to_img_str_cv2(image: PILImage.Image) -> str:
_, buffered = cv2.imencode(".png", cv2.cvtColor(np.array(image), cv2.COLOR_RGBA2BGRA))
img_str = base64.b64encode(buffered.tobytes()).decode("utf-8")
return img_str

@staticmethod
def _to_img_str_pil(image: PILImage.Image) -> str:
buffered = BytesIO()
image.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
return img_str

@classmethod
def from_pil(cls, image: PILImage.Image, dpi: int) -> Self:
"""Construct ImageRef from a PIL Image."""
if CV2_INSTALLED:
img_str = cls._to_img_str_cv2(image)
else:
img_str = cls._to_img_str_pil(image)
img_uri = f"data:image/png;base64,{img_str}"
return cls(
mimetype="image/png",
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ dev = [
'tree-sitter-java-orchard (>=0.3.0,<1.0.0); python_version >= "3.10"',
"ruff>=0.14.11",
"types-defusedxml (>=0.7.0.20250822, <0.8.0)",
"opencv-python-headless>=4.6.0.66,<5.0.0.0",
]
constraints = [
'pandas (>=2.1.4,<3.0.0); python_version < "3.11"',
Expand Down
21 changes: 21 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading