Display Base64 Image in DIAdem Report Using Python

Updated Apr 30, 2026

Environment

Software

  • DIAdem

Programming Language

  • Python

This article demonstrates how to use DIAdem to programmatically convert a Base64 string containing image data into an image object, and render the image within a report. It is common for image data to be stored in Base64 format when using internet protocols, like HTTP(s), to obtain and share images.

 

Before proceeding, note that:

 

To display a Base64 encoded image in a DIAdem report using a Python script, follow the steps below:

 

  1. In the DIAdem SCRIPT tab, click File >> New Python and save the file with a meaningful name.

 

 

  1. Underneath import sys (on line 7) add the following lines to import the following additionally required modules:
    • Refer to the top of this page for a summary of what each module is used for.
    • Note: these modules are installed by default with Python3.

 

import os
import base64
from io import BytesIO
import subprocess

 

  1. Underneath line 27 (where your user code should start), define a function called base64_to_bytes. This function takes a Base64 string as an input argument and performs the following actions:
    1. Check if the Base64 string input is preceded with data:image. If so, disregard this and extract only the Base64 data.
    2. Pass the Base64 string into the base64.b64decode function to decode it into image bytes.
    3. Return the image bytes out of the function.
    4. Note: the completed function should resemble the below code snippet.

 

# Function to decode Base64 string
def base64_to_bytes(base64_string):
    # Remove the data URI prefix if present
    if "data:image" in base64_string:
        base64_string = base64_string.split(",")[1]

    # Decode the Base64 string into bytes
    image_bytes = base64.b64decode(base64_string)
    return image_bytes

 

  1. Next, define a function called create_image_from_bytes. This function takes a Byte Object as an input argument and performs the following actions:
    1. Convert the Byte Object input into a BytesIO Object.
    2. Pass the BytesIO Object into the [External] Image.open function to open the image. Store the result in a variable called image.
    3. Return the image variable from the function.
    4. Note: the completed function should resemble the below code snippet.

 

# Function to create image from bytes
def create_image_from_bytes(image_bytes):
    # Create a BytesIO object to handle the image data
    image_stream = BytesIO(image_bytes)

    # Open the image using Pillow (PIL)
    image = Image.open(image_stream)
    return image

 

  1. After these function definitions, the main code can start. First, use the [External] subprocess.run function to install pillow and import the [External] Image module.
    • Note: if pillow is already installed on the system, the script will continue as normal.

 

# Make sure pillow module is installed and imported
subprocess.run(["pip", "install", "pillow"],stdout=subprocess.DEVNULL, text=True)
from PIL import Image

 

  1. Then, define a variable called base64_string to store your Base64 image data.
    • This tutorial uses the following Base64 string, which (when converted) corresponds to the NI logo seen directly below.

 

base64_string = "iVBORw0KGgoAAAANSUhEUgAAARUAAAC2CAMAAADAz+kkAAAAdVBMVEUDtYX///8AsoD5/v2K178AsHys5NTj8+2M07kAtIIAtoYAuYvt+fXr+va56tyi4c6/7OB/1bv0/Po3xJ5FyKR41ruT3cgavpNUyqm75tfQ8OZkyqqU2cIduo1JwZqH2sPd9u9CwJhbzq/T7+YtwJev59it4M7jox5tAAAEXUlEQVR4nO3dC3KbMBgEYEkYO7/fxk7t+J3Yyf2PWNyZTjvDthEICwntHqBevgokBEOUdsiLUW45rTevP5ajaZG51EDJBk7dXH7aWUWJiDH5djd428/aAvmVqFV+2xi5H47zoi2TXqg8Uo6a0/lypUolpcx62c6A6ZFKGcnXwzYGTL9UHgPmfTGlSiUyPn9SpRoxmxtVqhH54bSA6adK6bLbU6UaOV2oUo2Yj8aLl/6qKGXOTc+iPqsouS+pUo3ch1SpRsYLqlTTjKXvKkomDVh6r1KOlvqX3P6rlOu52hN0AipK3uvuuaSgouRQc5WbhIqSmvdEaagomVOlGpnU2m9JRKW8tNR5wJiKijq9UaUaWY+oUo1ZUQXFfuM/IRXZUQXF+jYxJRXZ2c7OKamok+1gSUpFVpZ3iWmpbC2noaRUlDlSpRrZ2r3bkpaKMnY7CompyAdVQMRqyZKcitUj1uRUDlSpRs42s1BqKupkMwslpyI2j52TUzEri1koORXZWDxeTU5FbS2exaenIhabLOmpmC+qVGNzuU1PRSxeTk5QZf396jZBFYudp/RUlKIKSE4VkMBVctM44qLy/eMPR5UXh7yuBo2zcmAx32/HOao0/982+arIGueah6ziEDNw+BTGiCpUoQpVQDmqoHJUQeWogspRBZWjCipHFVSOKqgcVVA5qqByVEHlqILKUQWVowoqRxVUjiqoHFVQOaqgclRB5aiCylEFlaMKKkcVVI4qqBxVUDmqoHJUQeWogspRBZWjCipHFVSOKqgcVVA5qqByVEHlqILKUQWVowoqRxVUjiqoHFVQOaqgclRB5aiCylEFlaMKKkcVVI4qqBxVUDmqoHJUQeWogspRBZWjCipHFVSOKqgcVVA5qqByVEHlQlY5UgXkjSpIxeHfd0rQKstJewdar1zIKvtTewdar1zIKre1y1fGHBK0ymxHlWoyl0+vuSRoFX2kCsiwo8tt2Cr7e3tHWqtc0Cr63M0pFLjKK1VAlh196zxslYxjBWXTCUvoKvNObptDVym2XQyW0FW62aUMXaWbPZbgVWaHDk6h4FX0cNze0VqXC15l1sGqP3wVvaAKysQ7SwwqQ++Tcwwqmfdlfwwq+s33NBSFSuF7myUKFX1998sSh4r+bO+IrcrFoaI/vM5DsaiMvO4oxKKi9yePLNGo6IXH6TkelcLjQ+d4VMolrrcrbkQqOrv7Gi0xqeiRrxuiqFT01RNLXCp65OckikxFT72s5mJT0cWhvYP/d7nYVPT08vzlXHwquhjKs8+iCFUeL4Y9mSVKFT1bPfdeMU4VredPfW07VhV9HYyfd1sUrYrWt53THxP/b7l4VbT+3D3psWLUKrqYHybPOI/iVinXdMvXSd76gIldRetsttialtd18as8cr2c72Npj6YfKuWI2S9eDvexMX9sTPPkKycVhx9uV+WR2W35ddxttpO8jFFDh9wcahQuPzxqTePvZEUxfeT2+RPjmnvNYbeirwAAAABJRU5ErkJggg=="

 

 

  1. Create a new Report Layout using the following command:

 

# Create new report
dd.Report.NewLayout()

 

  1. Call the base64_to_bytes and create_image_from_bytes functions defined in steps 3 and 4.

 

# Convert base64 data to image
image_bytes = base64_to_bytes(base64_string)
image = create_image_from_bytes(image_bytes)

 

  1. Save the newly created image object as a .png on disk using the [External] Image.save function. This will be deleted later.
    • This step is required because the DIAdem report can only load graphics by specifying a file path.

 

# Save image to file
image_filename = dd.CurrentScriptPath + "image.png"
image.save(image_filename)

 

  1. Create a new Image Object on the DIAdem report using the following line of code. This assigns the label Image1 to the object.

 

report_image = dd.Report.ActiveSheet.Objects.Add(dd.eReportObjectImage, "Image1")

 

  1. Set the FileName property of report_image so that the file saved in step 8 is loaded into the report.

 

report_image.FileName = image_filename

 

  1. Refresh the report to visualize changes.

 

dd.Report.Refresh()

 

  1. Use the ExportToPDF method to save the report as a PDF file. In the below code snippet, the PDF is named Report with Decoded Base64 Image.pdf and saved to the parent folder of the current script.

 

# Export the report
report_name = "Report with Decoded Base64 Image.pdf"
dd.Report.Sheets.ExportToPDF(dd.CurrentScriptPath + report_name,False)
dd.LogfileWrite("Report exported to: " + dd.CurrentScriptPath + report_name)

 

  1. Lastly, to clean up, use [External] os.remove to delete the .png file created in step 8.

 

# Delete image file
os.remove(dd.CurrentScriptPath + "image.png")

After following the steps above, the Python script should resemble the following:

 

import sys
import os
import base64
from io import BytesIO
import subprocess

if 'DIAdem' in sys.modules:
    from DIAdem import Application as dd

    if dd.AppEnableScriptDebugger:
        import debugpy
        debugpy.configure(python = sys.prefix + '\\python.exe')
        if not debugpy.is_client_connected():
            try:
                debugpy.listen(5678)
            except:
                pass
            debugpy.wait_for_client()

# --------------------------------------------------------------------
# Function to decode Base64 string
def base64_to_bytes(base64_string):
    # Remove the data URI prefix if present
    if "data:image" in base64_string:
        base64_string = base64_string.split(",")[1]

    # Decode the Base64 string into bytes
    image_bytes = base64.b64decode(base64_string)
    return image_bytes

# Function to create image from bytes
def create_image_from_bytes(image_bytes):
    # Create a BytesIO object to handle the image data
    image_stream = BytesIO(image_bytes)

    # Open the image using Pillow (PIL)
    image = Image.open(image_stream)
    return image

# --------------------------------------------------------------------

# Make sure pillow module is installed and imported
subprocess.run(["pip", "install", "pillow"],stdout=subprocess.DEVNULL, text=True)
from PIL import Image

# Get base64image data
base64_string = "iVBORw0KGgoAAAANSUhEUgAAARUAAAC2CAMAAADAz+kkAAAAdVBMVEUDtYX///8AsoD5/v2K178AsHys5NTj8+2M07kAtIIAtoYAuYvt+fXr+va56tyi4c6/7OB/1bv0/Po3xJ5FyKR41ruT3cgavpNUyqm75tfQ8OZkyqqU2cIduo1JwZqH2sPd9u9CwJhbzq/T7+YtwJev59it4M7jox5tAAA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wqQ++Tcwwqmfdlfwwq+s33NBSFSuF7myUKFX1998sSh4r+bO+IrcrFoaI/vM5DsaiMvO4oxKKi9yePLNGo6IXH6TkelcLjQ+d4VMolrrcrbkQqOrv7Gi0xqeiRrxuiqFT01RNLXCp65OckikxFT72s5mJT0cWhvYP/d7nYVPT08vzlXHwquhjKs8+iCFUeL4Y9mSVKFT1bPfdeMU4VredPfW07VhV9HYyfd1sUrYrWt53THxP/b7l4VbT+3D3psWLUKrqYHybPOI/iVinXdMvXSd76gIldRetsttialtd18as8cr2c72Npj6YfKuWI2S9eDvexMX9sTPPkKycVhx9uV+WR2W35ddxttpO8jFFDh9wcahQuPzxqTePvZEUxfeT2+RPjmnvNYbeirwAAAABJRU5ErkJggg=="
        
# Create new report
dd.Report.NewLayout()
        
# Convert base64 data to image
image_bytes = base64_to_bytes(base64_string)
image = create_image_from_bytes(image_bytes)
        
# Save image to file
image_filename = dd.CurrentScriptPath + "image.png"
image.save(image_filename)
        
# Add image to report layout
report_image = dd.Report.ActiveSheet.Objects.Add(dd.eReportObjectImage, "Image1")
report_image.FileName = image_filename
dd.Report.Refresh()
        
# Export the report
report_name = "Report with Decoded Base64 Image.pdf"
dd.Report.Sheets.ExportToPDF(dd.CurrentScriptPath + report_name,False)
dd.LogfileWrite("Report exported to: " + dd.CurrentScriptPath + report_name)
    
# Delete image file
os.remove(dd.CurrentScriptPath + "image.png")

 

After executing the script, a PDF report resembling the image below will be saved to disk:

 

Considerations if Using SystemLink

If you are defining a Base64 constant that never changes, this section can be ignored.

 

However, if you wish to use this solution for an Analysis Automation Procedure (ANP) in SystemLink where the Base64 string is dynamic, you may encounter issues when attempting to display large or high resolutions images in the report file. This is due to a character limitation in the ANP input parameters. An ANP parameter can have a maximum of 30000 characters. If your Base64 string exceeds this, some of the string will be cut off and the image will not load properly.