# How to access or save output image after running CLI from Python

**URL:** <https://discourse.slicer.org/t/how-to-access-or-save-output-image-after-running-cli-from-python/20494>\
**Category:** Development\
**Tags:** python, cli\
**Created:** [November 5, 2021, 3:27am UTC](https://discourse.slicer.org/t/how-to-access-or-save-output-image-after-running-cli-from-python/20494 "2021-11-05T03:27:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gnketiah](https://avatars.discourse-cdn.com/v4/letter/g/e480ec/32.png) [@gnketiah](https://discourse.slicer.org/u/gnketiah)\
**Post date:** [November 5, 2021, 3:27am UTC](https://discourse.slicer.org/t/how-to-access-or-save-output-image-after-running-cli-from-python/20494/1 "2021-11-05T03:27:45Z")

</div>

I have created or run a CLI from Python to calculate T1 mapping as follows.

```python
cliNode = slicer.cli.runSync(slicer.modules.t1mapping, None, parameters)

```

Detailed script:

```python
def t1mapping(parameters = {}):
    '''Code for running the pkmoddeling module with input parameters 
    specified in the dict'''
    # adding an alias to the module
    t1mapping = slicer.modules.t1mapping
    # creating a CLI node
    cliNode = slicer.cli.runSync(slicer.modules.t1mapping, None, parameters)
    if cliNode.GetStatus() & cliNode.ErrorsMask:
        # error
        errorText = cliNode.GetErrorText()
        slicer.mrmlScene.RemoveNode(cliNode)
        raise ValueError("CLI execution failed: " + errorText)
    return cliNode

def getIputVolume(inputdir,outputdir):
    DicomFolders=os.listdir(inputdir)
    for index,DicomFolder in enumerate(DicomFolders):
        fullPath = os.path.join(inputdir,DicomFolder)
        fileNames = os.listdir(fullPath)
        for index,fileName in enumerate(fileNames):
            fileNames[index]=os.path.join(inputdir,DicomFolder,fileName)
            
    db = slicer.dicomDatabase
    plugin = slicer.modules.dicomPlugins['MultiVolumeImporterPlugin']()
    if plugin:
        loadables = plugin.examine([fileNames])
    if len(loadables) == 0:
        print('plugin failed to interpret this series')
    else:
        patientID = db.fileValue(loadables[0].files[0],'0010,0020')
        outputPath = os.path.join(outputdir, patientID + ".nrrd") 
        print (outputPath)
        volume = plugin.load(loadables[0])
        slicer.util.saveNode(volume,outputPath) # saves the loaded image
 
    return volume, os.path.join(outputdir, patientID + "_T1map.nrrd") 

# get input volume and output path
inputImageVolume, outputFileName= getIputVolume(inputdir,outputdir)

# Parameters share for all patients in loop
defaultParameters = {'modelName': 'VFA'} 

# list containing parameterdict for each run of the code.
parameters = copy.copy(defaultParameters)
parameters['imageName'] = inputImageVolume 
parameters['T1MapFileName'] = outputFileName

# do actual stuff here  
t1mapping(parameters = parameters)

```

All parameters were set correctly and it runs without error (form cliNode.GetErrorText()). My question is how to access or save the calculated or resulting image from cliNode . I would prefer to it save as .nrrd or .mhd file.

I specified the full path in the parameters dict: e.g. parameters[‘T1MapFileName’] = ‘some\_path.nrrd’.  
But there is no output file or image.

Thanks

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [November 5, 2021, 3:55pm UTC](https://discourse.slicer.org/t/how-to-access-or-save-output-image-after-running-cli-from-python/20494/2 "2021-11-05T15:55:00Z")

</div>

`parameters['imageName']` and `parameters['T1MapFileName']` must be set to a valid MRML volume node object or MRML node ID. You need to create a new volume node for storing the output. See example [here](https://slicer.readthedocs.io/en/latest/developer_guide/python_faq.html#how-to-run-a-cli-module-from-python).

Once the CLI execution is completed, you can save the volume node to file using `saveNode` function. See example [here](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#save-volume-to-file).

---

<div class="post-metadata">

**Author:** ![gnketiah](https://avatars.discourse-cdn.com/v4/letter/g/e480ec/32.png) [@gnketiah](https://discourse.slicer.org/u/gnketiah)\
**Post date:** [November 15, 2021, 9:39am UTC](https://discourse.slicer.org/t/how-to-access-or-save-output-image-after-running-cli-from-python/20494/3 "2021-11-15T09:39:56Z")

</div>

Thanks for the guidance
