Downloading a File and PDF Image Extraction
Compatibility: API version 2 revision 21
A lab user can attach a PDF file containing multiple images to a result file placeholder, the script extracts the images which are automatically attached to corresponding samples as individual result files.
Images within a PDF may be in a number of formats, and will usually be .ppm or .jpeg. The example script includes additional code to convert .ppm images to .jpeg.
Prerequisites
You have the package 'poppler' (linux) installed.
You have defined a process with analytes (samples) as inputs, and outputs that generate the following:
A single shared result file output.
A result file output per input.
You have added samples to Clarity LIMS.
You have uploaded the Results PDF to Clarity LIMS during 'Step Setup'.
Optionally, if you wish to convert other file types to .jpeg, installation of ImageMagick (linux package).
Code Example
How it works:
The lab scientist runs a process/protocol step and attaches the PDF in Clarity LIMS
When run, the scrip uses the API and the 'requests' package available in python to locate and retrieve the PDF.
The script generates a file for each image.
Files are named with LUIDs and well location.
The images are attached to the ResultFile placeholder. **The file names must begin with the {outputFileLuids} for automatic attachment.**
Additionally, this script converts the images to JPEG format for compatibility with other LIMS features.
Step 1. Create the script
Part 1 - Downloading a file using the API
The script will find and get the content of the PDF through 2 separate GET requests:
Following the artifact URI using the {compoundOutputFile0} to identify the LUID of the PDF file.
Using the ~/api/v2/files/{luid}/download endpoint to save the file to the temporary working directory.
The PDF is written to the temporary directory.
The script performs a batch retrieval of the artifact XML for all samples. Subsequently a python dictionary is created defining which LIMS id corresponds to a given well location.
Part 2 - Extracting images as individual results files
The script uses the pdfimages function to extract the images from the PDF. This function is from a linux package and can be called using the the os.system() function.
This example script extracts an image from each page, beginning with page 10. Files are named with LUIDs and well location. The file names must begin with the {outputFileLuids} for automatic attachment.
Additionally, the cookbook example script converts the image files to JPEG for compatibility with other features in Clarity LIMS. The script uses 'convert', a function from the linux package called 'ImageMagick'. Like the 'pdfimages' function, 'convert' can be called in a python script through the os.system() function.
Step 2. Configure the Process
The steps required to configure a process to run EPP are described in the Process Execution with EPP/Automation Support example, namely:
Configure the inputs and outputs.
On the External Programs tab, select the check box to associate the process with an external program.
Parameters
The process parameter string for the external program is as follows:
The EPP command is configured to pass the following parameters:
-a
The limsid of the result file placeholder where the PDF is attached (Required)
-u
The username of the current user (Required)
-p
The password of the current user (Required)
-f
A list of the output LUIDS (required, must be in quotes "")
Step 3. Run the Process
Record Details page in Clarity LIMS
The placeholder where the lab scientist can upload the PDF.
External Process ready to be run. 'Script generated' message marking individual result file placeholder.
Expected Output and Results
External program was run successfully. Individual result files named with artifact LUID and well location.
Attachments
pdfimages.py:
Last updated