STL sliced/rasterized to BMP/TIFF stack

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

1 Like

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):

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.

1 Like

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?

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.

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.

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

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).

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.

1 Like

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?

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?

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.

1 Like

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

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.

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?

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.

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.

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.

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

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

Therefore I slice the STL-file with your phyton code. image_043
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.

1 Like

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

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\”, line 719, in loadModel
return loadNodeFromFile(filename, ‘ModelFile’, {}, returnNode)
File “C:\Users\chris\AppData\Local\NA-MIC\Slicer 4.11.20200930\bin\Python\slicer\”, 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:


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

Thanks in advance!