# 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:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![FLjosh](https://avatars.discourse-cdn.com/v4/letter/f/58f4c7/32.png) [@FLjosh](https://discourse.slicer.org/u/FLjosh)\
**Post date:** [December 2, 2019, 3:42am UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/1 "2019-12-02T03:42:02Z")

</div>

Hi There,  
I want to import an STL file and slice at a layer height of 0.1mm and generate a folder of TIFF images corresponding to a each fully dense layer. Is this possible?  
I have seen many people here trying to do the opposite (create an STL from a stack of images) so surely the reverse is possible! I just can’t quite figure out how to do that yet. Any tips greatly appreciated  
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:** [December 2, 2019, 4:50am UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/2 "2019-12-02T04:50:37Z")

</div>

Yes, you can do these kinds of conversions using Slicer. I’ve added an example to the script repository that does what you need (with customizable margin size and output label value):

[https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Rasterize\_a\_model\_and\_save\_it\_to\_a\_series\_of\_image\_files](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Rasterize_a_model_and_save_it_to_a_series_of_image_files)

You can copy-paste the code above to the Python console (Ctrl-3). You can of course do the same steps using GUI, let us know if you would prefer to do that instead of scripting.

---

<div class="post-metadata">

**Author:** ![FLjosh](https://avatars.discourse-cdn.com/v4/letter/f/58f4c7/32.png) [@FLjosh](https://discourse.slicer.org/u/FLjosh)\
**Post date:** [December 2, 2019, 9:09pm UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/3 "2019-12-02T21:09:37Z")

</div>

Thanks for that script! Looks just like what I need. Though having a bit of trouble implementing it.  
At line \>\>\> inputModel.GetBounds(bounds)  
I get an error AttributeError: ‘bool’ object has no attribute ‘GetBounds’  
Something I’m missing here?

---

<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:** [December 2, 2019, 9:26pm UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/4 "2019-12-02T21:26:10Z")

</div>

The script is to be used with recent Slicer Preview Releases. In latest stable, `loadModel` returns a bool flag (succes/fail) and you need to specify an additional argument to make it return the mode node as well. I would recommend you to use the latest preview release.

---

<div class="post-metadata">

**Author:** ![FLjosh](https://avatars.discourse-cdn.com/v4/letter/f/58f4c7/32.png) [@FLjosh](https://discourse.slicer.org/u/FLjosh)\
**Post date:** [December 2, 2019, 10:01pm UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/5 "2019-12-02T22:01:01Z")

</div>

Great thanks, I got to the bottom of the script. I assume in line  
outputLabelmapVolumeArray = (slicer.util.arrayFromVolume(outputLabelmapVolumeNode) \* labelValue).astype(‘int8’)  
That labelValue is meant to be declared outputVolumeLabelValue  
For a 55x40x40mm part I get 126 images which are all just black 128x131 pixels. Was expecting to get black silhouette of slice on white background right? Sorry - learning and asking at the same time.

---

<div class="post-metadata">

**Author:** ![FLjosh](https://avatars.discourse-cdn.com/v4/letter/f/58f4c7/32.png) [@FLjosh](https://discourse.slicer.org/u/FLjosh)\
**Post date:** [December 2, 2019, 10:32pm UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/6 "2019-12-02T22:32:03Z")

</div>

O got it to work! Just figured out the SpacingMm and MarginMm variables. Thanks for that!  
Is there a way to increase resolution to around 600dpi and assign colours to background and sliced 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:** [December 3, 2019, 3:48am UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/7 "2019-12-03T03:48:48Z")

</div>

> [@FLjosh](#):
>
> increase resolution to around 600dpi

You can set any resolution by adjusting outputVolumeSpacingMm values (first two values are image spacing within a slice, third value is image spacing between slices).

> [@FLjosh](#):
>
> assign colours to background and sliced image

It is an indexed image, so it has no colors. You can create an RGB image by defining a lookup table and running through `np.take` (e.g, something like this: `outputVolumeLabelValue=1; ... lut=[[255,0,0],[0,255,0]]; imageRGB = np.take(lut, imageIndexed)`). If you are doing this for 3D printing colored bitmaps on polyjet printers then you may consider using [SlicerFab extension](https://github.com/SlicerFab/SlicerFab).

---

<div class="post-metadata">

**Author:** ![FLjosh](https://avatars.discourse-cdn.com/v4/letter/f/58f4c7/32.png) [@FLjosh](https://discourse.slicer.org/u/FLjosh)\
**Post date:** [December 4, 2019, 12:50am UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/8 "2019-12-04T00:50:30Z")

</div>

Thanks @lassoan. It is for 3D printing but just inverted the matrix as I just needed black on white.  
I have considerably increased the “resolution” by changing SpacingMm and have a high quality STL. However curves and diagonals are represented very coarse (45deg line steps up in 5x5pix increments).  
Does one of the functions quantize the model heavily?

---

<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:** [December 4, 2019, 7:55am UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/9 "2019-12-04T07:55:12Z")

</div>

If you import a model into segmentation without specifying reference geometry then a default resolution is chosen automatically. You can force the segmentation’s internal labelmap representation geometry to be the same as the output geometry by calling ` seg.SetReferenceImageGeometryParameterFromVolumeNode(referenceVolumeNode)` after `AddNewNodeByClass('vtkMRMLSegmentationNode')`

Do you use a Stratasys printer? Can you write a bit about your application? My understanding is that voxel printing’s main advantages are that you can use voxel intensity of original images (and/or information extracted from them), you can generate 3D prints with regions of non-uniform color/opacity/stiffness, save time on segmentation, and create more detailed prints (e.g., using SlicerFab or similar methods), but you cannot really benefit from these if the bitmap just contains uniform regions defined by STL files. Or you just want to use these to validate your workflow vs. directly printing an STL file?

---

<div class="post-metadata">

**Author:** ![FLjosh](https://avatars.discourse-cdn.com/v4/letter/f/58f4c7/32.png) [@FLjosh](https://discourse.slicer.org/u/FLjosh)\
**Post date:** [December 4, 2019, 9:28pm UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/10 "2019-12-04T21:28:19Z")

</div>

Thanks that worked, though I placed the call earlier, just after creating the seg object.  
It is for a DIY binder-jetting printer (only jetting binder not color). But another person designed the printhead interface which takes bitmap images layer by layer. I couldn’t find any software which generates these images except 3DSlicer.

---

<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:** [December 4, 2019, 9:32pm UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/11 "2019-12-04T21:32:47Z")

</div>

Sounds very exciting project. It’s great that you could make it all work.

---

<div class="post-metadata">

**Author:** ![JSchnecke](https://avatars.discourse-cdn.com/v4/letter/j/d78d45/32.png) [@JSchnecke](https://discourse.slicer.org/u/JSchnecke)\
**Post date:** [June 8, 2020, 8:19am UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/12 "2020-06-08T08:19:00Z")

</div>

I am also new to the programm and community.  
Thank you for the code! I need the code for my masterthesis.  
I use the code you wrote, but when I am copying it into the python command box, 3D Slicer crashes and closes the window without saying something. I used the versions 4.10.2 and 4.11.0.

Maybe you have an idea, what I am doing wrong.

---

<div class="post-metadata">

**Author:** ![Heimdal](https://avatars.discourse-cdn.com/v4/letter/h/9de0a6/32.png) [@Heimdal](https://discourse.slicer.org/u/Heimdal)\
**Post date:** [August 10, 2020, 5:48pm UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/13 "2020-08-10T17:48:11Z")

</div>

I have the same trouble as JSchnecke. When I am copying the code in the command box 3D Slicer chrashes down.  
I need TIFF-files from an STL for a 3D printing job.  
Can you help me please?

---

<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:** [August 10, 2020, 9:43pm UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/14 "2020-08-10T21:43:34Z")

</div>

What command exactly?

For tiff to STL conversion you don’t have to write any code but you can load the TIFF stack as a label volume, right-click in Data module to convert to segmentation, then use “Export to files” to save as STL. If the TIFF images are not yet segmented then you can segment using Segment Editor module.

---

<div class="post-metadata">

**Author:** ![Heimdal](https://avatars.discourse-cdn.com/v4/letter/h/9de0a6/32.png) [@Heimdal](https://discourse.slicer.org/u/Heimdal)\
**Post date:** [August 12, 2020, 9:27pm UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/15 "2020-08-12T21:27:22Z")

</div>

Thank you Andras for the great tool. Now it is running.  
I have one question more. Everything in the tif-file is grey and everything outside is black. For my printing job I need the reagions inside black and outside white. Can I configure something in your code that I get tif-files out in the form I need.  
Thanks for your help.

---

<div class="post-metadata">

**Author:** ![Heimdal](https://avatars.discourse-cdn.com/v4/letter/h/9de0a6/32.png) [@Heimdal](https://discourse.slicer.org/u/Heimdal)\
**Post date:** [August 13, 2020, 7:11am UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/16 "2020-08-13T07:11:55Z")

</div>

I shouldn’t be writing messages late at night. Sorry for the errors in the above text.  
What I want to say is that everything inside the object in the tiff file is grey and everything outside is black. For my printer I need black inside and white outside. Can I change something in your code to get the Tiff in the desired format?  
Thanks for your help.

---

<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:** [August 13, 2020, 1:15pm UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/17 "2020-08-13T13:15:52Z")

</div>

Can you attach a few screenshots that show what you have now and mark on it what you would like to be different?

---

<div class="post-metadata">

**Author:** ![Heimdal](https://avatars.discourse-cdn.com/v4/letter/h/9de0a6/32.png) [@Heimdal](https://discourse.slicer.org/u/Heimdal)\
**Post date:** [August 13, 2020, 2:57pm UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/18 "2020-08-13T14:57:48Z")

</div>

For example, I would like to print the goose as you can see in the picture.

 ![Ente](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/d/9/d9e189c37fa9b9cb761da916f9880de0b83221d2.jpeg)

Therefore I slice the STL-file with your phyton code. ![image_043](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/e/4/e4febcacce08999ff34ea26453914326cda5fe27.jpeg)  
In the black and white (I think in real it is grey) picture you can see one tiff-file (the feet of the goose) of the slicing result from your python code. So far so good. Unfortunately I need for my 3D printer the black areas in white and the white (grey) areas in black. That means that at the moment my printer would print the surrounding of the goose and the goose itself would not be printed.

What I need is shown in the picture below.  
 ![image_043_invert](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/2/12c9a57d3e977293d30878cad6321c4a42b55032.jpeg)

---

<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:** [August 13, 2020, 3:52pm UTC](https://discourse.slicer.org/t/stl-sliced-rasterized-to-bmp-tiff-stack/9351/19 "2020-08-13T15:52:06Z")

</div>

You can use Simple Filters module’s IntensityWindowingImageFilter method to rescale and invert intensity (set Output minimum \> Output maximum to invert intensity).

---

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

</div>

Hi @lassoan

Thanks for sharing the STL to TIFF stack Python script. I’m running into an error when running the following line:

inputModel = slicer.util.loadModel(inputModelFile)

The error:

Traceback (most recent call last):  
File “”, line 1, in   
File “C:\Users\chris\AppData\Local\NA-MIC\Slicer 4.11.20200930\bin\Python\slicer\util.py”, line 719, in loadModel  
return loadNodeFromFile(filename, ‘ModelFile’, {}, returnNode)  
File “C:\Users\chris\AppData\Local\NA-MIC\Slicer 4.11.20200930\bin\Python\slicer\util.py”, line 598, in loadNodeFromFile  
raise RuntimeError(errorMessage)  
RuntimeError: Failed to load node from file: F:\APEP\Internal\_CA\_Project\Method\_ValidationD\_Sphereox\_20x20x10.stl

The original filepath of my .stl was:

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

which didn’t run into any issues. Any thoughts?

Thanks in advance!

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