# STL sliced/rasterized to BMP/TIFF stack

**URL:** <https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351>\
**Category:** Support\
**Created:** [December 2, 2019, 3:42am UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351 "2019-12-02T03:42:02Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [October 21, 2020, 5:44pm UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/22 "2020-10-21T17:44:01Z")

</div>

> [@chris-is-pinbo](#):
>
> RuntimeError: Failed to load node from file: F:\APEP\Internal\_CA\_Project\Method\_ValidationD\_Sphereox\_20x20x10.stl

You are missing a

> \

between your Validation and 3DSphere.

---

<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:** [October 21, 2020, 5:48pm UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/23 "2020-10-21T17:48:04Z")

</div>

Also note that you need to be careful when using backslash in a string in Python:

> [@Subprocess.call does not work!](https://discourse.slicer.org/t/subprocess-call-does-not-work/2837/14):
>
> FYI, since backslash (\) is an escape character in Python, you cannot simply use it in a string literal in Python. The easiest is to indicate that you specify a raw string by prepending an r character before the string. This is correct: somePath = r"F:\someFolder\python.exe" If you need to use a regular string then backslash can be entered by typing double-backslash. This is correct: somePath = "F:\\someFolder\\python.exe" You may also use Unix-type separators. This is correct: somePath = …

---

<div class="post-metadata">

**Author:** ![chris-is-pinbo](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chris-is-pinbo/32/8541_2.png) [@chris-is-pinbo](https://discourse.slicer.org/u/chris-is-pinbo)\
**Post date:** [October 21, 2020, 5:49pm UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/24 "2020-10-21T17:49:39Z")

</div>

Hi @muratmaga, thanks for the quick reply. I don’t understand how that has happened because

inputModelFile = F:\APEP\Internal\_CA\_Project\Method\_Validation\3D\_Sphere\box\_20x20x10.stl

and inputModelFile is the input to slicer.util.loadModel(), which generated the error. It seems like my file path is being altered in some way.

---

<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:** [October 21, 2020, 5:50pm UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/25 "2020-10-21T17:50:37Z")

</div>

> [@chris-is-pinbo](#):
>
> It seems like my file path is being altered in some way.

See the explanation [above](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/23).

---

<div class="post-metadata">

**Author:** ![chris-is-pinbo](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chris-is-pinbo/32/8541_2.png) [@chris-is-pinbo](https://discourse.slicer.org/u/chris-is-pinbo)\
**Post date:** [October 21, 2020, 5:54pm UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/26 "2020-10-21T17:54:41Z")

</div>

Hi @lassoan,

Thanks for the suggestion to use

```auto
somePath = r"F:\someFolder\python.exe"

```

That seemed to do the trick. Now I’m running into an issue during the last loop, where the script writes to the output directory.

(Note: I haven’t edited anything in the file but the input and output filepaths. The physical dimensions of the geometry I am looking to convert to a .tiff stack aren’t important as I am just interested in the curvature of the model.)

Here’s what the Python console is showing me:

```auto
>>> inputModelFile = r"F:\APEP\Internal_CA_Project\Method_Validation\3D_Sphere\box_20x20x10.stl"
>>> outputDir = r"F:\APEP\Internal_CA_Project\Method_Validation\3D_Sphere\tiffs"
>>> outputVolumeLabelValue = 100
>>> outputVolumeSpacingMm = [0.5, 0.5, 0.5]
>>> outputVolumeMarginMm = [10.0, 10.0, 10.0]
>>> 
>>> # Read model
>>> inputModel = slicer.util.loadModel(inputModelFile)
>>> 
>>> # Determine output volume geometry and create a corresponding reference volume
>>> import math
>>> import numpy as np
>>> bounds = np.zeros(6)
>>> inputModel.GetBounds(bounds)
>>> imageData = vtk.vtkImageData()
>>> imageSize = [int((bounds[axis*2+1]-bounds[axis*2]+outputVolumeMarginMm[axis]*2.0)/outputVolumeSpacingMm[axis]) for axis in range(3) ]
>>> imageOrigin = [bounds[axis*2]-outputVolumeMarginMm[axis] for axis in range(3) ]
>>> imageData.SetDimensions(imageSize)
>>> imageData.AllocateScalars(vtk.VTK_UNSIGNED_CHAR, 1)
>>> imageData.GetPointData().GetScalars().Fill(0)
>>> referenceVolumeNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLScalarVolumeNode")
>>> referenceVolumeNode.SetOrigin(imageOrigin)
>>> referenceVolumeNode.SetSpacing(outputVolumeSpacingMm)
>>> referenceVolumeNode.SetAndObserveImageData(imageData)
>>> referenceVolumeNode.CreateDefaultDisplayNodes()
>>> 
>>> # Convert model to labelmap
>>> seg = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLSegmentationNode')
>>> seg.SetReferenceImageGeometryParameterFromVolumeNode(referenceVolumeNode)
>>> slicer.modules.segmentations.logic().ImportModelToSegmentationNode(inputModel, seg)
True
>>> seg.CreateBinaryLabelmapRepresentation()
True
>>> outputLabelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode')
>>> slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(seg, outputLabelmapVolumeNode, referenceVolumeNode)
True
>>> outputLabelmapVolumeArray = (slicer.util.arrayFromVolume(outputLabelmapVolumeNode) * outputVolumeLabelValue).astype('int8')
>>> 
>>> # Write labelmap volume to series of TIFF files
>>> pip_install("imageio")
Requirement already satisfied: imageio in c:\users\chris\appdata\local\na-mic\slicer 4.11.20200930\lib\python\lib\site-packages (2.9.0)
Requirement already satisfied: pillow in c:\users\chris\appdata\local\na-mic\slicer 4.11.20200930\lib\python\lib\site-packages (from imageio) (7.2.0)
Requirement already satisfied: numpy in c:\users\chris\appdata\local\na-mic\slicer 4.11.20200930\lib\python\lib\site-packages (from imageio) (1.19.1)
WARNING: You are using pip version 20.1.1; however, version 20.2.4 is available.
You should consider upgrading via the 'C:\Users\chris\AppData\Local\NA-MIC\Slicer 4.11.20200930\bin\python-real.exe -m pip install --upgrade pip' command.
>>> import imageio
>>> for i in range(len(outputLabelmapVolumeArray)):
... imageio.imwrite(f'{outputDir}/image_{i:03}.tiff', outputLabelmapVolumeArray[i])
... 
Traceback (most recent call last):
  File "<console>", line 2, in <module>
  File "C:\Users\chris\AppData\Local\NA-MIC\Slicer 4.11.20200930\lib\Python\Lib\site-packages\imageio\core\functions.py", line 303, in imwrite
    writer = get_writer(uri, format, "i", **kwargs)
  File "C:\Users\chris\AppData\Local\NA-MIC\Slicer 4.11.20200930\lib\Python\Lib\site-packages\imageio\core\functions.py", line 217, in get_writer
    request = Request(uri, "w" + mode, **kwargs)
  File "C:\Users\chris\AppData\Local\NA-MIC\Slicer 4.11.20200930\lib\Python\Lib\site-packages\imageio\core\request.py", line 124, in __init__
    self._parse_uri(uri)
  File "C:\Users\chris\AppData\Local\NA-MIC\Slicer 4.11.20200930\lib\Python\Lib\site-packages\imageio\core\request.py", line 265, in _parse_uri
    raise FileNotFoundError("The directory %r does not exist" % dn)
FileNotFoundError: The directory 'F:\\APEP\\Internal_CA_Project\\Method_Validation\\3D_Sphere\\tiffs' does not exist
>>> 

```

---

<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:** [October 21, 2020, 6:53pm UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/27 "2020-10-21T18:53:19Z")

</div>

The output directory (`F:\\APEP\\Internal_CA_Project\\Method_Validation\\3D_Sphere\\tiffs`) must exist. You need to create it either by adding a line to the Python script or manually (using Windows File Explorer, etc).

---

<div class="post-metadata">

**Author:** ![chris-is-pinbo](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chris-is-pinbo/32/8541_2.png) [@chris-is-pinbo](https://discourse.slicer.org/u/chris-is-pinbo)\
**Post date:** [October 21, 2020, 7:25pm UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/28 "2020-10-21T19:25:45Z")

</div>

😳 what a simple issue. Thanks for the help @lassoan

---

<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 1, 2020, 3:40pm UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/29 "2020-11-01T15:40:34Z")

</div>

A post was split to a new topic: [Using Slicer via web API](https://discourse.slicer.org/t/using-slicer-via-web-api/14368)

---

<div class="post-metadata">

**Author:** ![heimlifeiss](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@heimlifeiss](https://discourse.slicer.org/u/heimlifeiss)\
**Post date:** [January 12, 2022, 5:54pm UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/30 "2022-01-12T17:54:37Z")

</div>

Hello  
I’m absolutly new to slicer and I downloaded it mainly because of the problem discussed here.

I want to do the same thing: slice an STL and then save each layer as a bpm or pdf or something else. This problem here is solved with a script. My skills in programming and similar stuff are quite limited. So, is it also possible to slice the STL in the GUI/slicer program and save each layer? Right away I didn’t find such a function so I wanted to ask.

Would be great if I could get some help here. I also need these layers for 3d printing.  
Thanks 🙂

---

<div class="post-metadata">

**Author:** ![Jack-Waghorne](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jack-waghorne/32/15209_2.png) [@Jack-Waghorne](https://discourse.slicer.org/u/Jack-Waghorne)\
**Post date:** [May 4, 2022, 9:52am UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/31 "2022-05-04T09:52:00Z")

</div>

Hello,

I am following this script but Slicer crash whenever the following line is entered:  
slicer.modules.segmentations.logic().ImportModelToSegmentationNode(inputModel, seg)  
If you can provide any guidance on this it would be greatly appreciated.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![robert](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/robert/32/65410_2.png) [@robert](https://discourse.slicer.org/u/robert)\
**Post date:** [March 28, 2023, 3:15am UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/32 "2023-03-28T03:15:47Z")

</div>

Hi, It’s what I am looking for and I want to export one 3D tif file（contain all 2D tifs）. But I can’t find the link to get the script example. How do I get it?  
Besides, how to use GUI to realize it？ Great 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:** [March 28, 2023, 3:26am UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/33 "2023-03-28T03:26:02Z")

</div>

The script is now available [here](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#rasterize-a-model-and-save-it-to-a-series-of-image-files).

[Previous page](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351.md?page=1)
