In this example, we’ll use DocParse to extract an image from a battery manual. We’ll go through the important code snippets below to see what’s going on.Let’s focus on the following code that makes a call to Aryn DocParse API to extract the image from the PDF:
add_document.py
Copy
Ask AI
import aryn_sdkfrom aryn_sdk.partition import partition_file, tables_to_pandasimport pandas as pdfrom io import BytesIOfile = open('my-document.pdf', 'rb')aryn_api_key = 'YOUR-KEY-HERE'## Make a call to Aryn DocParse ## param extract_images (boolean): extract image contents. default: False## returns: JSON object with elements representing information inside the PDFpartitioned_file = partition_file(file, aryn_api_key, extract_images=True)
If you inspect the partitioned_file variable, you’ll notice that it’s a large JSON object with details about all the components in the PDF (checkout this page to understand the schema of the returned JSON object in detail). Below, we highlight the Image element that contains the information about some of the images in the page:
This JSON object represents one of the images in the PDF. You’ll notice that the image’s binary representation, its bounding box (which indicates the coordinates of the image in the PDF), and certain other properties (image_mode, image_size etc.) are returned back. You can then process this JSON however you’d like for further analysis. In the notebook, we use the Pillow Image module from python to display the extracted image on its own.
image_output.py
Copy
Ask AI
## extract all the images from the JSON and print out the JSON representation of the first imageimages = [e for e in partitioned_file['elements'] if e['type'] == 'Image']first_image = images[0]## read in the image and display itimage_width = first_image['properties']['image_size'][0]image_height = first_image['properties']['image_size'][1]image_mode = first_image['properties']['image_mode']image = Image.frombytes(image_mode, (image_width, image_height), base64.b64decode(first_image['binary_representation']))#display the imageimage