To display a Base64 encoded image in a DIAdem report using a Python script, follow the steps below:
- In the DIAdem SCRIPT tab, click File >> New Python and save the file with a meaningful name.

- 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
- 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:
- Check if the Base64 string input is preceded with data:image. If so, disregard this and extract only the Base64 data.
- Pass the Base64 string into the base64.b64decode function to decode it into image bytes.
- Return the image bytes out of the function.
- 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
- Next, define a function called create_image_from_bytes. This function takes a Byte Object as an input argument and performs the following actions:
- Convert the Byte Object input into a BytesIO Object.
- Pass the BytesIO Object into the [External] Image.open function to open the image. Store the result in a variable called image.
- Return the image variable from the function.
- 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
- 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
- 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/Po3xJ5FyKR41ruT3cgavpNUyqm75tfQ8OZky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MXaWbPZbgVWaHDk6h4FX0cNze0VqXC15l1sGqP3wVvaAKysQ7SwwqQ++Tcwwqmfdlfwwq+s33NBSFSuF7myUKFX1998sSh4r+bO+IrcrFoaI/vM5DsaiMvO4oxKKi9yePLNGo6IXH6TkelcLjQ+d4VMolrrcrbkQqOrv7Gi0xqeiRrxuiqFT01RNLXCp65OckikxFT72s5mJT0cWhvYP/d7nYVPT08vzlXHwquhjKs8+iCFUeL4Y9mSVKFT1bPfdeMU4VredPfW07VhV9HYyfd1sUrYrWt53THxP/b7l4VbT+3D3psWLUKrqYHybPOI/iVinXdMvXSd76gIldRetsttialtd18as8cr2c72Npj6YfKuWI2S9eDvexMX9sTPPkKycVhx9uV+WR2W35ddxttpO8jFFDh9wcahQuPzxqTePvZEUxfeT2+RPjmnvNYbeirwAAAABJRU5ErkJggg=="

- Create a new Report Layout using the following command:
# Create new report
dd.Report.NewLayout()
- 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)
- 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)
- 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")
- 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
- Refresh the report to visualize changes.
dd.Report.Refresh()
- 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)
- 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")