# Multiply two images using "Multiply Scalar Volumes" or "Mask Scalar Volume" and save the resulting image (using Python)

**URL:** <https://discourse.slicer.org/t/multiply-two-images-using-multiply-scalar-volumes-or-mask-scalar-volume-and-save-the-resulting-image-using-python/10947>\
**Category:** Support\
**Tags:** masking\
**Created:** [March 31, 2020, 8:49pm UTC](https://discourse.slicer.org/t/multiply-two-images-using-multiply-scalar-volumes-or-mask-scalar-volume-and-save-the-resulting-image-using-python/10947 "2020-03-31T20:49:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vishal\_P](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/vishal_p/32/6438_2.png) [@Vishal\_P](https://discourse.slicer.org/u/Vishal_P)\
**Post date:** [March 31, 2020, 8:49pm UTC](https://discourse.slicer.org/t/multiply-two-images-using-multiply-scalar-volumes-or-mask-scalar-volume-and-save-the-resulting-image-using-python/10947/1 "2020-03-31T20:49:37Z")

</div>

I am a beginner trying to automate some tasks in 3D Slicer using Python.

I have a mask image and another input volume image both in nifti format.  
how can I use python to automate the multiplication between two images and save the output to a folder?

so far I was able to load the two images.

```python
slicer.mrmlScene.Clear(False)
[success, volume] = slicer.util.loadVolume(filename = 'stripped_AX_FLAIR_stretched.nii',returnNode=True) 
slicer.app.layoutManager().setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutOneUpRedSliceView)
[success, labelmapVolumeNode] = slicer.util.loadLabelVolume(filename = 'Edema_AX_FLAIR_label.nii', returnNode=True)

```

---

<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:** [April 1, 2020, 2:28am UTC](https://discourse.slicer.org/t/multiply-two-images-using-multiply-scalar-volumes-or-mask-scalar-volume-and-save-the-resulting-image-using-python/10947/2 "2020-04-01T02:28:35Z")

</div>

You can use `arrayFromVolume` function as shown in this [example in the script repository](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Combine_multiple_volumes_into_one).

---

<div class="post-metadata">

**Author:** ![Vishal\_P](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/vishal_p/32/6438_2.png) [@Vishal\_P](https://discourse.slicer.org/u/Vishal_P)\
**Post date:** [April 1, 2020, 9:01pm UTC](https://discourse.slicer.org/t/multiply-two-images-using-multiply-scalar-volumes-or-mask-scalar-volume-and-save-the-resulting-image-using-python/10947/3 "2020-04-01T21:01:33Z")

</div>

Thanks for the reply Lasso, Certainly this is the way to proceed. but while executing the script. i get an error that the segment volume dimension is not aligned.

“”"  
ValueError: shapes (26,512,512) and (13,179,150) not aligned: 512 (dim 2) != 179 (dim 1)

“”"  
what can i do to overcome this error?

---

<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:** [April 1, 2020, 9:32pm UTC](https://discourse.slicer.org/t/multiply-two-images-using-multiply-scalar-volumes-or-mask-scalar-volume-and-save-the-resulting-image-using-python/10947/4 "2020-04-01T21:32:31Z")

</div>

It seems that the two volumes have different sizes. How did you create them?

---

<div class="post-metadata">

**Author:** ![Vishal\_P](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/vishal_p/32/6438_2.png) [@Vishal\_P](https://discourse.slicer.org/u/Vishal_P)\
**Post date:** [April 1, 2020, 9:52pm UTC](https://discourse.slicer.org/t/multiply-two-images-using-multiply-scalar-volumes-or-mask-scalar-volume-and-save-the-resulting-image-using-python/10947/5 "2020-04-01T21:52:11Z")

</div>

initially, I had a master\_volume file and a label\_file (which contains multiple labels).

to extract segment\_volumes of each individual label from the label\_file, I separated each label from label\_file using the segment editor module and converted them back to individual label files and saved them to my folder directory.  
(this process has been executed completely in Python)

Currently, I am trying to automate the task of masking the individual labels (extracted from label\_file) on to the master\_volume to extract the segment\_volumes using python and save them as a nifti file.

for example  
the first image is the individual label separated from label\_file.  
the second image is the result when masked with master\_volume.

 ![Screen Shot 2020-03-23 at 4.07.37 PM](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/c/9/c9d836eeeb815ccc4ceecf028fd60a60d0175995.png)

---

<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:** [April 1, 2020, 10:46pm UTC](https://discourse.slicer.org/t/multiply-two-images-using-multiply-scalar-volumes-or-mask-scalar-volume-and-save-the-resulting-image-using-python/10947/6 "2020-04-01T22:46:56Z")

</div>

> [@Vishal\_P](#):
>
> to extract segment\_volumes of each individual label from the label\_file, I separated each label from label\_file using the segment editor module and converted them back to individual label files and saved them to my folder directory.

This should not be necessary. Instead, [export the segmentation to labelmap volume](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Export_labelmap_node_from_segmentation_node) (you can specify the volume node that you want to combine it with as reference volume) and then this exported labelmap volume will have the correct geometry. Using numpy, you can very easily extract a single label value, combine it with another array, etc.

---

<div class="post-metadata">

**Author:** ![Vishal\_P](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/vishal_p/32/6438_2.png) [@Vishal\_P](https://discourse.slicer.org/u/Vishal_P)\
**Post date:** [April 2, 2020, 1:21am UTC](https://discourse.slicer.org/t/multiply-two-images-using-multiply-scalar-volumes-or-mask-scalar-volume-and-save-the-resulting-image-using-python/10947/7 "2020-04-02T01:21:44Z")

</div>

i am using slicer 4.10.2  
in MacOS Catalina

```auto
seg_node = slicer.vtkMRMLSegmentationNode()
        slicer.mrmlScene.AddNode(seg_node)
        seg = seg_node.GetSegmentation()
        seg.CopySegmentFromSegmentation(segmentation,segmentation.GetNthSegmentID(i))
        lab_node = slicer.vtkMRMLLabelMapVolumeNode()
        slicer.mrmlScene.AddNode(lab_node)
        slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(seg_node,lab_node,volume)

```

the code works fine till i reach the last line, when i execute ExportVisibleSegmentsTo LabelMapNode, my jupyter notebook kernel dies and 3d slicer crashes giving me a prompt to reopen 3d slicer.

---

<div class="post-metadata">

**Author:** ![Vishal\_P](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/vishal_p/32/6438_2.png) [@Vishal\_P](https://discourse.slicer.org/u/Vishal_P)\
**Post date:** [April 2, 2020, 3:12am UTC](https://discourse.slicer.org/t/multiply-two-images-using-multiply-scalar-volumes-or-mask-scalar-volume-and-save-the-resulting-image-using-python/10947/8 "2020-04-02T03:12:54Z")

</div>

Got the desired result using “ExportSegmentsToLabelmapNode” and by declaring “reference volume” dimensions are preserved.  
As the dimensions are equal now, I was able to perform the multiplication operation.

Thanks for your guidance…! 🙂

> arr\_vt = vtk.vtkStringArray()  
> arr\_vt.InsertNextValue(segmentation.GetNthSegmentID(0))  
> result = arr\_vt.GetValue(0)  
> print(result)

> slicer.modules.segmentations.logic().ExportSegmentsToLabelmapNode(seg\_node,arr\_vt,lab\_node,volume)

> vol\_1=slicer.util.getNode(‘str\*’)  
> vol\_2=slicer.util.getNode(‘LabelMapVolume’)  
> a = slicer.util.arrayFromVolume(vol\_1)  
> b = slicer.util.arrayFromVolume(vol\_2)  
> c = a \* b

> volumeNode\_result = slicer.modules.volumes.logic().CloneVolume(volume, “multiply”)  
> slicer.util.updateVolumeFromArray(volumeNode\_result, c)  
> setSliceViewerLayers(background=volumeNode\_result)

---

<div class="post-metadata">

**Author:** ![Ramadhan\_Gunia](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ramadhan_gunia/32/15803_2.png) [@Ramadhan\_Gunia](https://discourse.slicer.org/u/Ramadhan_Gunia)\
**Post date:** [July 26, 2022, 8:14pm UTC](https://discourse.slicer.org/t/multiply-two-images-using-multiply-scalar-volumes-or-mask-scalar-volume-and-save-the-resulting-image-using-python/10947/9 "2022-07-26T20:14:20Z")

</div>

Hello, kindly assist me with the whole code you used to solve the problem “[Multiply two images using “Multiply Scalar Volumes” or “Mask Scalar Volume” and save the resulting image (using Python)”
