Docling Image Extraction#
Extracting images from documents in Docling involves two coordinated steps: enabling image generation in PdfPipelineOptions so that pixel data is retained during conversion, and then accessing that data through PictureItem (or page objects) in the resulting DoclingDocument.
1. Pipeline Configuration#
Three fields on PdfPipelineOptions (and its parent PaginatedPipelineOptions) control image generation :
| Field | Default | Effect |
|---|---|---|
generate_page_images | False | Rasterizes each full page; required for DocItem.get_image() to work on any element type |
generate_picture_images | False | Stores a dedicated cropped image on each PictureItem.image (ImageRef) directly |
images_scale | 1.0 | Resolution multiplier; 2.0 is the CLI default and a practical maximum (values above 2.0 may cause bugs) |
Both flags default to False; no pixel data is kept unless at least one is set. generate_page_images = True is sufficient for PictureItem.get_image(doc) because the method falls back to cropping the page image when self.image is None .
Minimal setup to enable image extraction:
from docling.datamodel.pipeline_options import PdfPipelineOptions
from docling.document_converter import DocumentConverter, PdfFormatOption
from docling.datamodel.base_models import InputFormat
pipeline_options = PdfPipelineOptions()
pipeline_options.images_scale = 2.0
pipeline_options.generate_page_images = True
pipeline_options.generate_picture_images = True # optional: stores image per PictureItem
converter = DocumentConverter(
format_options={InputFormat.PDF: PdfFormatOption(pipeline_options=pipeline_options)}
)
result = converter.convert("document.pdf")
See export_figures.py for the canonical example.
Note:
generate_picture_imagesis flagged for deprecation in v3; prefergenerate_page_images+get_image()going forward.
2. Accessing Images via PictureItem#
PictureItem extends FloatingItem which inherits from DocItem. The key access patterns are:
PictureItem.get_image(doc, prov_index=0) → Optional[PIL.Image.Image]
Defined on FloatingItem: returns self.image.pil_image if generate_picture_images was set; otherwise falls back to cropping the page raster via DocItem.get_image(). Returns None if no page image is available.
PictureItem.image → Optional[ImageRef]
Populated only when generate_picture_images = True. ImageRef stores mimetype, dpi, size, and a uri (data URL or file path). The PIL image is lazily decoded via ImageRef.pil_image .
PictureItem.meta → Optional[PictureMeta]
Holds enrichment results: classification (predicted image type + confidence), description (VLM caption), tabular_chart, and molecule. Populated only if the corresponding pipeline flags (do_picture_classification, do_picture_description, etc.) are enabled .
PictureItem.caption_text(doc) → str
Resolves linked caption items and returns concatenated caption text .
Iterating all pictures in a document:
for picture in result.document.pictures:
img = picture.get_image(result.document) # PIL Image or None
if img:
img.save(f"picture-{picture.self_ref}.png")
print(picture.caption_text(result.document))
if picture.meta and picture.meta.classification:
print(picture.meta.classification)
See inspect_picture_content.py for traversing child text items within a picture region.
3. Saving and Exporting Images#
Save individual images to disk — call element.get_image(doc).save(fp, "PNG") inside an iterate_items() loop .
Save page-level rasters — access page.image.pil_image from doc.pages .
Export to Markdown/HTML with embedded or referenced images — use ImageRefMode :
from docling_core.types.doc import ImageRefMode
doc.save_as_markdown(path, image_mode=ImageRefMode.EMBEDDED) # base64 inline
doc.save_as_markdown(path, image_mode=ImageRefMode.REFERENCED) # external PNG files
doc.save_as_html(path, image_mode=ImageRefMode.REFERENCED)
Per-item export is also available directly on PictureItem :
picture.export_to_markdown(doc, image_mode=ImageRefMode.EMBEDDED)picture.export_to_html(doc, image_mode=ImageRefMode.PLACEHOLDER)(default for HTML)
4. Optional Enrichments#
Enable additional enrichments in PdfPipelineOptions before conversion :
| Flag | What it populates |
|---|---|
do_picture_classification=True | PictureItem.meta.classification — image type labels + confidence scores |
do_picture_description=True | PictureItem.meta.description — VLM-generated caption text |
chart_extraction_model=ChartExtractionModelKind.GRANITE_VISION | PictureItem.meta.tabular_chart — chart data in tabular form |
Key Source Files#
| File | Role |
|---|---|
docling/datamodel/pipeline_options.py | PdfPipelineOptions: generate_page_images, generate_picture_images, images_scale |
docling_core/types/doc/items/node.py | DocItem.get_image(), FloatingItem.get_image(), FloatingItem.image |
docling_core/types/doc/items/picture/picture.py | PictureItem, export_to_markdown(), export_to_html(), PictureMeta |
docling_core/types/doc/common/reference.py | ImageRef, PageItem |
docs/examples/export_figures.py | End-to-end: page/picture/table image export + Markdown/HTML with ImageRefMode |
docs/examples/inspect_picture_content.py | Iterating picture regions and their child text items |