# How to detect MRI file being loaded?

**URL:** <https://discourse.slicer.org/t/how-to-detect-mri-file-being-loaded/45079>\
**Category:** Development\
**Tags:** mri, dicom\
**Created:** [November 14, 2025, 10:31am UTC](https://discourse.slicer.org/t/how-to-detect-mri-file-being-loaded/45079 "2025-11-14T10:31:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Victor\_Wayne](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/victor_wayne/32/80778_2.png) [@Victor\_Wayne](https://discourse.slicer.org/u/Victor_Wayne)\
**Post date:** [November 14, 2025, 10:31am UTC](https://discourse.slicer.org/t/how-to-detect-mri-file-being-loaded/45079/1 "2025-11-14T10:31:48Z")

</div>

Hello,

I need to log the header of the MRI file. How can I detect if an MRI file is loaded in slicer?

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![ebrahim](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ebrahim/32/13403_2.png) [@ebrahim](https://discourse.slicer.org/u/ebrahim)\
**Post date:** [November 14, 2025, 4:34pm UTC](https://discourse.slicer.org/t/how-to-detect-mri-file-being-loaded/45079/2 "2025-11-14T16:34:09Z")

</div>

If you need to do something whenever a node is added, you can listen for the `slicer.vtkMRMLScene.NodeAddedEvent`. [Example](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#show-volume-rendering-automatically-when-a-volume-is-loaded)

What kind of information is in the header depends on the file format, so you would need to figure out how your file format indicates whether the modality is MRI. Not all formats indicate this information in any standard way.

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [November 17, 2025, 10:27am UTC](https://discourse.slicer.org/t/how-to-detect-mri-file-being-loaded/45079/3 "2025-11-17T10:27:23Z")

</div>

It is also possible to add a callback for DICOM image having been loaded. Let me know if that’s what you want instead.

---

<div class="post-metadata">

**Author:** ![Victor\_Wayne](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/victor_wayne/32/80778_2.png) [@Victor\_Wayne](https://discourse.slicer.org/u/Victor_Wayne)\
**Post date:** [November 18, 2025, 4:31am UTC](https://discourse.slicer.org/t/how-to-detect-mri-file-being-loaded/45079/4 "2025-11-18T04:31:23Z")

</div>

The user will be working only with .nii files. (Most of the times atleast)

---

<div class="post-metadata">

**Author:** ![Victor\_Wayne](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/victor_wayne/32/80778_2.png) [@Victor\_Wayne](https://discourse.slicer.org/u/Victor_Wayne)\
**Post date:** [November 18, 2025, 4:33am UTC](https://discourse.slicer.org/t/how-to-detect-mri-file-being-loaded/45079/5 "2025-11-18T04:33:11Z")

</div>

Can you please elaborate on that?

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [November 18, 2025, 11:30am UTC](https://discourse.slicer.org/t/how-to-detect-mri-file-being-loaded/45079/6 "2025-11-18T11:30:10Z")

</div>

You can add this in `.slicerrc.py` or if you have a custom module then to its initialization:

```auto
    dicomWidget = slicer.modules.dicom.widgetRepresentation()
    dicomWidgetSelf = dicomWidget.self()
    # Override onLoadingFinished to use customized function
    dicomWidgetSelf.browserWidget.onLoadingFinished = self.onDicomLoadingFinished

```

Then you can implement `onDicomLoadingFinished` as you desire. You can check the modality of the DICOM, and it it’s an MRI, then you can do what you need with that (you provided very little information on what you want to do).

---

<div class="post-metadata">

**Author:** ![Victor\_Wayne](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/victor_wayne/32/80778_2.png) [@Victor\_Wayne](https://discourse.slicer.org/u/Victor_Wayne)\
**Post date:** [November 21, 2025, 5:21am UTC](https://discourse.slicer.org/t/how-to-detect-mri-file-being-loaded/45079/7 "2025-11-21T05:21:42Z")

</div>

Thanks for your help.  
I have only one more question.  
Is there any way to find out if the vtkMRMLScalarVolumeNode came from a nifty file or a dicom file?

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [November 21, 2025, 9:33am UTC](https://discourse.slicer.org/t/how-to-detect-mri-file-being-loaded/45079/8 "2025-11-21T09:33:19Z")

</div>

Yes. If loaded from DICOM, the volume node and its subject hierarchy item has all kinds of attributes

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/e/b/eb5a4ad4caa373699c50f0ff93c8ac2002682e57.png)

If loaded from file, the volume node’s storage node has the file name.

---

<div class="post-metadata">

**Author:** ![Victor\_Wayne](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/victor_wayne/32/80778_2.png) [@Victor\_Wayne](https://discourse.slicer.org/u/Victor_Wayne)\
**Post date:** [November 21, 2025, 9:40am UTC](https://discourse.slicer.org/t/how-to-detect-mri-file-being-loaded/45079/9 "2025-11-21T09:40:31Z")

</div>

Thank you so much for your help.
