# Importing DICOM files in my own module C++

**URL:** <https://discourse.slicer.org/t/importing-dicom-files-in-my-own-module-c/12455>\
**Category:** Development\
**Created:** [July 9, 2020, 3:47am UTC](https://discourse.slicer.org/t/importing-dicom-files-in-my-own-module-c/12455 "2020-07-09T03:47:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DavidCai1246](https://avatars.discourse-cdn.com/v4/letter/d/e99b99/32.png) [@DavidCai1246](https://discourse.slicer.org/u/DavidCai1246)\
**Post date:** [July 9, 2020, 3:47am UTC](https://discourse.slicer.org/t/importing-dicom-files-in-my-own-module-c/12455/1 "2020-07-09T03:47:25Z")

</div>

I would like to import DICOM files in my own module by calling the import function in the “DICOM” module without actually switching to the “DICOM” module. Is this possible to do?

I was thinking of calling the import functions in the “DICOM” module in the backend, but the DICOM module is written in python and my module is in C++.

The python code is something like this:

> plugin = slicer.modules.dicomPlugins’DICOMScalarVolumePlugin’  
> loadables = plugin.examineFiles(dicom\_fileList)  
> if len(loadables) \> 1:  
> print(“Multiple DICOM scans detected! Not proceeding”)  
> sys.exit(1)  
> volume = plugin.load(loadables[0])

---

<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:** [July 9, 2020, 1:43pm UTC](https://discourse.slicer.org/t/importing-dicom-files-in-my-own-module-c/12455/2 "2020-07-09T13:43:00Z")

</div>

The DICOM browser and indexer are written in C++, so you can access them directly from C++. DICOM _loader_ plugins are mostly implemented in Python, so you need to call Python from C++.

You can either use CPython or PythonQt API. For example, to run a Python command and get value of a variable using PythonQt API:

> <https://github.com/Slicer/Slicer/blob/master/Base/QTGUI/qSlicerSettingsGeneralPanel.cxx#L99-L102>

---

<div class="post-metadata">

**Author:** ![DavidCai1246](https://avatars.discourse-cdn.com/v4/letter/d/e99b99/32.png) [@DavidCai1246](https://discourse.slicer.org/u/DavidCai1246)\
**Post date:** [July 9, 2020, 8:21pm UTC](https://discourse.slicer.org/t/importing-dicom-files-in-my-own-module-c/12455/3 "2020-07-09T20:21:44Z")

</div>

Thank you very much!

---

<div class="post-metadata">

**Author:** ![DavidCai1246](https://avatars.discourse-cdn.com/v4/letter/d/e99b99/32.png) [@DavidCai1246](https://discourse.slicer.org/u/DavidCai1246)\
**Post date:** [July 10, 2020, 6:52pm UTC](https://discourse.slicer.org/t/importing-dicom-files-in-my-own-module-c/12455/4 "2020-07-10T18:52:31Z")

</div>

I’m currently trying to use PythonQt in my project. One issue I found is that I cannot pass arguments into my python script. my python script that I want to run is:

```
def dicom2file(path_to_dicom_dir,output_path):
    
## Grab indivial DICOM slices into a filelist
excluded_extensions = ['.txt', '.csv', '.xml', '.tag', '.nii']
dicom_fileList = [os.path.join(path_to_dicom_dir,file) for file in os.listdir(path_to_dicom_dir)
                    if not file.endswith( tuple(excluded_extensions) ) ]
# Loading DICOM using the ScalarVolumePlugin
plugin = slicer.modules.dicomPlugins['DICOMScalarVolumePlugin']()
loadables = plugin.examineFiles(dicom_fileList)
if len(loadables) > 1:
    print("Multiple DICOM scans detected! Not proceeding")
    sys.exit(1)
volume = plugin.load(loadables[0])

## Save the DICOM volume in scalar volume format
slicer.util.saveNode(volume, output_path)

## To-do: exits should be move to high-level wrapper
#exit()

```

I need to pass in 2 arguments `path_to_dicom_dir` and `output_path`  
Is there a way for me to pass arguments into my python script in C++ with pythonQT or should I use something else?

---

<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:** [July 10, 2020, 8:18pm UTC](https://discourse.slicer.org/t/importing-dicom-files-in-my-own-module-c/12455/5 "2020-07-10T20:18:10Z")

</div>

You can set simple input values by writing their values in the executed string. For setting more complicated input objects or getting output objects, you can add/get variables in the context. See lots of examples in [qSlicerDICOMExportDialog.cxx](https://github.com/Slicer/Slicer/blob/master/Modules/Scripted/DICOMLib/Widgets/qSlicerDICOMExportDialog.cxx).

---

<div class="post-metadata">

**Author:** ![DavidCai1246](https://avatars.discourse-cdn.com/v4/letter/d/e99b99/32.png) [@DavidCai1246](https://discourse.slicer.org/u/DavidCai1246)\
**Post date:** [July 10, 2020, 9:05pm UTC](https://discourse.slicer.org/t/importing-dicom-files-in-my-own-module-c/12455/6 "2020-07-10T21:05:48Z")

</div>

I will look into it, thank you so much!
