docling icon indicating copy to clipboard operation
docling copied to clipboard

extraction of images

Open madmaxtwentytwo opened this issue 1 year ago • 3 comments

can docling be used for extraction of images from documents? if yes provide me the code samples so that i can extract those images into a folder and then i can run an ocr by myseld

madmaxtwentytwo avatar Aug 18 '25 03:08 madmaxtwentytwo

Yes, Docling can extract images from documents like PDF, DOCX, HTML, and AsciiDoc. Images are represented as "picture" items in the document model, and you can save them to a folder for OCR or other processing. Here’s a code sample based on the official example for PDFs, but the approach is similar for other formats:

from pathlib import Path
from docling.datamodel.base_models import InputFormat
from docling.datamodel.pipeline_options import PdfPipelineOptions
from docling.document_converter import DocumentConverter, PdfFormatOption
from docling_core.types.doc import PictureItem, TableItem

input_doc_path = Path("your_document.pdf")  # Change to your file
output_dir = Path("output_images")
output_dir.mkdir(parents=True, exist_ok=True)

pipeline_options = PdfPipelineOptions()
pipeline_options.images_scale = 2  # Adjust image resolution if needed
pipeline_options.generate_page_images = True
pipeline_options.generate_picture_images = True

doc_converter = DocumentConverter(
    format_options={InputFormat.PDF: PdfFormatOption(pipeline_options=pipeline_options)}
)
conv_res = doc_converter.convert(input_doc_path)
doc_filename = conv_res.input.file.stem

# Save images of figures and tables
table_counter = 0
picture_counter = 0
for element, _level in conv_res.document.iterate_items():
    if isinstance(element, TableItem):
        table_counter += 1
        img_path = output_dir / f"{doc_filename}-table-{table_counter}.png"
        element.get_image(conv_res.document).save(img_path, "PNG")
    if isinstance(element, PictureItem):
        picture_counter += 1
        img_path = output_dir / f"{doc_filename}-picture-{picture_counter}.png"
        element.get_image(conv_res.document).save(img_path, "PNG")

You’ll need to install docling and pillow (for image saving):

pip install docling pillow

For DOCX, HTML, or AsciiDoc, just use the corresponding input format and backend; the extraction logic is similar—iterate over PictureItem objects and save their images. The images will be in your output folder, ready for OCR or any other processing you want to do.

See the full example and more details in the export_figures.py script.

To reply, just mention @dosu.


Docs are dead. Just use Dosu.

Leave Feedback Ask Dosu about docling Share Dosu with your team

dosubot[bot] avatar Aug 18 '25 03:08 dosubot[bot]

give me a code for universal upload where we can add any type of document like ppt or docs or pdf etc

madmaxtwentytwo avatar Aug 18 '25 03:08 madmaxtwentytwo

Here's a Python script for a "universal upload" that accepts any supported document type (PDF, DOCX, PPTX, etc.) and extracts images to a folder using Docling. The DocumentConverter automatically detects the format and routes processing to the right backend. You can pass any file path, and it will handle the rest:

from pathlib import Path
from docling.document_converter import DocumentConverter
from docling_core.types.doc import PictureItem, TableItem

def extract_images_from_any_document(input_doc_path, output_dir):
    input_doc_path = Path(input_doc_path)
    output_dir = Path(output_dir)
    output_dir.mkdir(parents=True, exist_ok=True)

    doc_converter = DocumentConverter()  # Supports all formats by default
    conv_res = doc_converter.convert(input_doc_path)
    doc_filename = conv_res.input.file.stem

    table_counter = 0
    picture_counter = 0
    for element, _level in conv_res.document.iterate_items():
        if isinstance(element, TableItem):
            table_counter += 1
            img_path = output_dir / f"{doc_filename}-table-{table_counter}.png"
            element.get_image(conv_res.document).save(img_path, "PNG")
        if isinstance(element, PictureItem):
            picture_counter += 1
            img_path = output_dir / f"{doc_filename}-picture-{picture_counter}.png"
            element.get_image(conv_res.document).save(img_path, "PNG")

# Example usage:
extract_images_from_any_document("your_file.pptx", "output_images")
extract_images_from_any_document("your_file.docx", "output_images")
extract_images_from_any_document("your_file.pdf", "output_images")

Install dependencies:

pip install docling pillow

Docling supports PDF, DOCX, PPTX, HTML, AsciiDoc, and more. For DOCX, only bitmap images are extracted (DrawingML images aren't supported). PPTX support is available, but extraction may depend on how images are embedded. The same logic works for all formats—just pass the file path and output folder.

For more details on supported formats and backend selection, see the DocumentConverter implementation.

To reply, just mention @dosu.


Docs are dead. Just use Dosu.

Leave Feedback Ask Dosu about docling Share Dosu with your team

dosubot[bot] avatar Aug 18 '25 03:08 dosubot[bot]