Detect text in a document: Bounds

Returns the bounds for the boxes around the text detected in a document.

Documentation pages that include this code sample

To view the code sample used in context, see the following documentation:

Code sample


Before trying this sample, follow the Python setup instructions in the Vision quickstart using client libraries. For more information, see the Vision Python API reference documentation.

def get_document_bounds(image_file, feature):
    """Returns document bounds given an image."""
    client = vision.ImageAnnotatorClient()

    bounds = []

    with, "rb") as image_file:
        content =

    image = vision.Image(content=content)

    response = client.document_text_detection(image=image)
    document = response.full_text_annotation

    # Collect specified feature bounds by enumerating all document features
    for page in document.pages:
        for block in page.blocks:
            for paragraph in block.paragraphs:
                for word in paragraph.words:
                    for symbol in word.symbols:
                        if feature == FeatureType.SYMBOL:

                    if feature == FeatureType.WORD:

                if feature == FeatureType.PARA:

            if feature == FeatureType.BLOCK:

    # The list `bounds` contains the coordinates of the bounding boxes.
    return bounds

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.