461bf6fd40
CI / lint (3.11) (push) Has been cancelled
CI / lint (3.12) (push) Has been cancelled
CI / lint (3.13) (push) Has been cancelled
CI / shellcheck (push) Has been cancelled
CI / shfmt (push) Has been cancelled
CI / setup (3.11) (push) Has been cancelled
CI / setup (3.12) (push) Has been cancelled
CI / setup (3.13) (push) Has been cancelled
CI / check-licenses (3.12) (push) Has been cancelled
CI / test_unit (3.11) (push) Has been cancelled
CI / test_unit (3.12) (push) Has been cancelled
CI / test_unit (3.13) (push) Has been cancelled
CI / test_unit_no_extras (3.11) (push) Has been cancelled
CI / test_unit_no_extras (3.12) (push) Has been cancelled
CI / test_json_to_html (3.12) (push) Has been cancelled
CI / test_unit_no_extras (3.13) (push) Has been cancelled
CI / test_unit_dependency_extras (csv, 3.12, --extra csv) (push) Has been cancelled
CI / test_unit_dependency_extras (xlsx, 3.11, --extra xlsx) (push) Has been cancelled
CI / test_unit_dependency_extras (xlsx, 3.12, --extra xlsx) (push) Has been cancelled
CI / test_unit_dependency_extras (csv, 3.11, --extra csv) (push) Has been cancelled
CI / test_unit_dependency_extras (csv, 3.13, --extra csv) (push) Has been cancelled
CI / test_unit_dependency_extras (docx, 3.11, --extra docx) (push) Has been cancelled
CI / test_unit_dependency_extras (docx, 3.12, --extra docx) (push) Has been cancelled
CI / test_unit_dependency_extras (docx, 3.13, --extra docx) (push) Has been cancelled
CI / test_unit_dependency_extras (markdown, 3.11, --extra md) (push) Has been cancelled
CI / test_unit_dependency_extras (markdown, 3.12, --extra md) (push) Has been cancelled
CI / test_unit_dependency_extras (markdown, 3.13, --extra md) (push) Has been cancelled
CI / test_unit_dependency_extras (odt, 3.11, --extra odt) (push) Has been cancelled
CI / test_unit_dependency_extras (odt, 3.12, --extra odt) (push) Has been cancelled
CI / test_unit_dependency_extras (odt, 3.13, --extra odt) (push) Has been cancelled
CI / test_unit_dependency_extras (pdf-image, 3.11, --extra pdf --extra image --extra paddleocr) (push) Has been cancelled
CI / test_unit_dependency_extras (pdf-image, 3.12, --extra pdf --extra image --extra paddleocr) (push) Has been cancelled
CI / test_unit_dependency_extras (pdf-image, 3.13, --extra pdf --extra image --extra paddleocr) (push) Has been cancelled
CI / test_unit_dependency_extras (pptx, 3.11, --extra pptx) (push) Has been cancelled
CI / test_unit_dependency_extras (pptx, 3.12, --extra pptx) (push) Has been cancelled
CI / test_unit_dependency_extras (pptx, 3.13, --extra pptx) (push) Has been cancelled
CI / test_unit_dependency_extras (pypandoc, 3.11, --extra epub --extra org --extra rtf --extra rst) (push) Has been cancelled
CI / test_unit_dependency_extras (pypandoc, 3.12, --extra epub --extra org --extra rtf --extra rst) (push) Has been cancelled
CI / test_unit_dependency_extras (pypandoc, 3.13, --extra epub --extra org --extra rtf --extra rst) (push) Has been cancelled
Build And Push Docker Image / set-short-sha (push) Has been cancelled
Partition Benchmark / setup (push) Has been cancelled
Partition Benchmark / Measure and compare partition() runtime (push) Has been cancelled
CI / test_unit_dependency_extras (xlsx, 3.13, --extra xlsx) (push) Has been cancelled
CI / test_ingest_src (3.12) (push) Has been cancelled
CI / test_json_to_markdown (3.12) (push) Has been cancelled
CI / changelog (push) Has been cancelled
CI / test_dockerfile (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Build And Push Docker Image / build-images (linux/amd64, opensource-linux-8core) (push) Has been cancelled
Build And Push Docker Image / build-images (linux/arm64, ubuntu-24.04-arm) (push) Has been cancelled
Build And Push Docker Image / publish-images (push) Has been cancelled
124 lines
5.7 KiB
Python
124 lines
5.7 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import IO, Any, Optional
|
|
|
|
from unstructured.chunking import add_chunking_strategy
|
|
from unstructured.documents.elements import Element, process_metadata
|
|
from unstructured.file_utils.filetype import add_metadata
|
|
from unstructured.partition.common.common import exactly_one
|
|
from unstructured.partition.common.lang import check_language_args
|
|
from unstructured.partition.pdf import partition_pdf_or_image
|
|
from unstructured.partition.utils.constants import PartitionStrategy
|
|
|
|
|
|
@process_metadata() # TODO(shreya): update to use `apply_metadata` decorator
|
|
@add_metadata
|
|
@add_chunking_strategy
|
|
def partition_image(
|
|
filename: Optional[str] = None,
|
|
file: Optional[IO[bytes]] = None,
|
|
include_page_breaks: bool = False,
|
|
infer_table_structure: bool = False,
|
|
ocr_languages: Optional[str] = None,
|
|
languages: Optional[list[str]] = None,
|
|
detect_language_per_element: bool = False,
|
|
strategy: str = PartitionStrategy.HI_RES,
|
|
metadata_last_modified: Optional[str] = None,
|
|
chunking_strategy: Optional[str] = None,
|
|
hi_res_model_name: Optional[str] = None,
|
|
extract_images_in_pdf: bool = False,
|
|
extract_image_block_types: Optional[list[str]] = None,
|
|
extract_image_block_output_dir: Optional[str] = None,
|
|
extract_image_block_to_payload: bool = False,
|
|
starting_page_number: int = 1,
|
|
extract_forms: bool = False,
|
|
form_extraction_skip_tables: bool = True,
|
|
password: Optional[str] = None,
|
|
**kwargs: Any,
|
|
) -> list[Element]:
|
|
"""Parses an image into a list of interpreted elements.
|
|
|
|
Parameters
|
|
----------
|
|
filename
|
|
A string defining the target filename path.
|
|
file
|
|
A file-like object as bytes --> open(filename, "rb").
|
|
include_page_breaks
|
|
If True, includes page breaks at the end of each page in the document.
|
|
infer_table_structure
|
|
Only applicable if `strategy=hi_res`.
|
|
If True, any Table elements that are extracted will also have a metadata field
|
|
named "text_as_html" where the table's text content is rendered into an html string.
|
|
I.e., rows and cells are preserved.
|
|
Whether True or False, the "text" field is always present in any Table element
|
|
and is the text content of the table (no structure).
|
|
languages
|
|
The languages present in the document, for use in partitioning and/or OCR. To use a language
|
|
with Tesseract, you'll first need to install the appropriate Tesseract language pack.
|
|
strategy
|
|
The strategy to use for partitioning the image. Valid strategies are "hi_res" and
|
|
"ocr_only". When using the "hi_res" strategy, the function uses a layout detection
|
|
model if to identify document elements. When using the "ocr_only" strategy,
|
|
partition_image simply extracts the text from the document using OCR and processes it.
|
|
The default strategy is `hi_res`.
|
|
metadata_last_modified
|
|
The last modified date for the document.
|
|
hi_res_model_name
|
|
The layout detection model used when partitioning strategy is set to `hi_res`.
|
|
extract_images_in_pdf
|
|
Only applicable if `strategy=hi_res`.
|
|
If True, any detected images will be saved in the path specified by
|
|
'extract_image_block_output_dir' or stored as base64 encoded data within metadata fields.
|
|
Deprecation Note: This parameter is marked for deprecation. Future versions will use
|
|
'extract_image_block_types' for broader extraction capabilities.
|
|
extract_image_block_types
|
|
Only applicable if `strategy=hi_res`.
|
|
Images of the element type(s) specified in this list (e.g., ["Image", "Table"]) will be
|
|
saved in the path specified by 'extract_image_block_output_dir' or stored as base64 encoded
|
|
data within metadata fields.
|
|
extract_image_block_to_payload
|
|
Only applicable if `strategy=hi_res`.
|
|
If True, images of the element type(s) defined in 'extract_image_block_types' will be
|
|
encoded as base64 data and stored in two metadata fields: 'image_base64' and
|
|
'image_mime_type'.
|
|
This parameter facilitates the inclusion of element data directly within the payload,
|
|
especially for web-based applications or APIs.
|
|
extract_image_block_output_dir
|
|
Only applicable if `strategy=hi_res` and `extract_image_block_to_payload=False`.
|
|
The filesystem path for saving images of the element type(s)
|
|
specified in 'extract_image_block_types'.
|
|
extract_forms
|
|
Whether the form extraction logic should be run
|
|
(results in adding FormKeysValues elements to output).
|
|
form_extraction_skip_tables
|
|
Whether the form extraction logic should ignore regions designated as Tables.
|
|
password
|
|
The password to decrypt the PDF file.
|
|
"""
|
|
exactly_one(filename=filename, file=file)
|
|
|
|
languages = check_language_args(languages or [], ocr_languages)
|
|
|
|
return partition_pdf_or_image(
|
|
filename=filename,
|
|
file=file,
|
|
is_image=True,
|
|
include_page_breaks=include_page_breaks,
|
|
infer_table_structure=infer_table_structure,
|
|
languages=languages,
|
|
detect_language_per_element=detect_language_per_element,
|
|
strategy=strategy,
|
|
metadata_last_modified=metadata_last_modified,
|
|
hi_res_model_name=hi_res_model_name,
|
|
extract_images_in_pdf=extract_images_in_pdf,
|
|
extract_image_block_types=extract_image_block_types,
|
|
extract_image_block_output_dir=extract_image_block_output_dir,
|
|
extract_image_block_to_payload=extract_image_block_to_payload,
|
|
starting_page_number=starting_page_number,
|
|
extract_forms=extract_forms,
|
|
form_extraction_skip_tables=form_extraction_skip_tables,
|
|
password=password,
|
|
**kwargs,
|
|
)
|