# Create TIFF stack from an stl file

**URL:** <https://discourse.slicer.org/t/create-tiff-stack-from-an-stl-file/26650>\
**Category:** Development\
**Created:** [December 8, 2022, 2:13pm UTC](https://discourse.slicer.org/t/create-tiff-stack-from-an-stl-file/26650 "2022-12-08T14:13:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gcoopermax](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gcoopermax](https://discourse.slicer.org/u/gcoopermax)\
**Post date:** [December 8, 2022, 2:13pm UTC](https://discourse.slicer.org/t/create-tiff-stack-from-an-stl-file/26650/1 "2022-12-08T14:13:21Z")

</div>

Hello

I want to create a stack of TIFF images from an stl file and have tried the code provided in the post below however I can’t get this to work as I keep getting error “[VTK] No input data”. Can you please help with this?

> [@Convert STL file to high-resolution TIFF stack](https://discourse.slicer.org/t/convert-stl-file-to-high-resolution-tiff-stack/15949/7):
>
> I only open slicer (version 4.11.20200930 ) and paste the following script into the python interactor window: import math import numpy as np import sys np.set\_printoptions(threshold=sys.maxsize) inputModelFile = "C:\\\Users\\\User\\\Documents\\\Slicer\\\test area.stl" outputDir = "C:\\\Users\\\User\\\Documents\\\Slicer\\\Tiff" outputVolumeLabelValue = 255 #contrast?? 255= white outputVolumeSpacingMm = [0.0635, 0.0635, 0.1] #mm steps/resolution 400dpi =15.748dot/mm =0.0635 outputVolumeMarginMm …

I couldn’t find a way to upload the stl file

```auto

inputModelFile = r"C:\Work\Temp\Body1.stl"
outputDir = r"C:\Work\Temp\Slicer"
outputVolumeLabelValue = 255
inPlaneResolutionDpi = 200
planeThicknessMm = 1
outputVolumeMarginMm = [0, 0, 0] # mm of buffer before/after and around

import math
import numpy as np
import sys
try:
    import imageio
except ImportError:
    pip_install("imageio")

imageSpacingMm = [25.4/inPlaneResolutionDpi, 25.4/inPlaneResolutionDpi, planeThicknessMm]
inputModel = slicer.util.loadModel(inputModelFile)
bounds = np.zeros(6)
inputModel.GetBounds(bounds)
imageSize = [int((math.ceil(bounds[axis*2+1] - bounds[axis*2] + 2 * outputVolumeMarginMm[axis]))/imageSpacingMm[axis]) for axis in range(3) ]
imageOrigin = [bounds[axis*2] - outputVolumeMarginMm[axis] for axis in range(3) ]
sliceImageData = vtk.vtkImageData()
sliceImageData.SetDimensions(imageSize[0], imageSize[1], 1)
sliceImageData.AllocateScalars(vtk.VTK_UNSIGNED_CHAR, 1)
sliceImageData.GetPointData().GetScalars().Fill(0)
sliceNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLLabelMapVolumeNode")
sliceNode.SetOrigin(imageOrigin)
sliceNode.SetSpacing(imageSpacingMm)
sliceNode.SetAndObserveImageData(sliceImageData)
sliceNode.CreateDefaultDisplayNodes()
seg = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLSegmentationNode')
seg.SetReferenceImageGeometryParameterFromVolumeNode(sliceNode)
# restrict rasterization to the reference geometry (single frame at a time)
seg.GetSegmentation().SetConversionParameter("Crop to reference image geometry", "1")
seg.GetSegmentation().SetMasterRepresentationName("Closed surface")
slicer.modules.segmentations.logic().ImportModelToSegmentationNode(inputModel, seg)
segmentId = seg.GetSegmentation().GetNthSegmentID(0)
seg.CreateBinaryLabelmapRepresentation()

for sliceIndex in range(imageSize[2]):
    sliceNode.SetOrigin(imageOrigin[0], imageOrigin[1], imageOrigin[2] + sliceIndex * imageSpacingMm[2])
    seg.SetReferenceImageGeometryParameterFromVolumeNode(sliceNode)
    seg.GetSegmentation().CreateRepresentation('Binary labelmap', True) # Force recreating binary labelmap representation immediately
    outputSliceImageArray = np.squeeze(slicer.util.arrayFromSegmentBinaryLabelmap(seg, segmentId))
    outputSliceImageArray[outputSliceImageArray>0] = outputVolumeLabelValue
    filename = f'{outputDir}/slice_{sliceIndex:03}.tif'
    result = imageio.imwrite(filename, outputSliceImageArray)
    print(filename)
    slicer.app.processEvents() # update console

```

---

<div class="post-metadata">

**Author:** ![gcoopermax](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gcoopermax](https://discourse.slicer.org/u/gcoopermax)\
**Post date:** [December 8, 2022, 2:20pm UTC](https://discourse.slicer.org/t/create-tiff-stack-from-an-stl-file/26650/2 "2022-12-08T14:20:26Z")

</div>

I have attached the file here.

> **[Body1.stl](https://drive.google.com/file/d/1ewMUciJCosxstz9XmxrUI7DD4uC-Uxw-/view?usp=share_link)**
>
> Google Drive file.

---

<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 8, 2022, 5:00pm UTC](https://discourse.slicer.org/t/create-tiff-stack-from-an-stl-file/26650/3 "2022-12-08T17:00:53Z")

</div>

I’ve tried and the script works well on your data.

You can ignore the `[VTK] No input data` message, it is just logged when a slice view does not intersect the segmentation node and so there is nothing to show. We’ll suppress this message to not confuse users.
