You are missing a
\
between your Validation and 3DSphere.
You are missing a
\
between your Validation and 3DSphere.
Also note that you need to be careful when using backslash in a string in Python:
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.
See the explanation above.
Hi @lassoan,
Thanks for the suggestion to use
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:
>>> 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
>>>
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).
what a simple issue. Thanks for the help @lassoan
A post was split to a new topic: Using Slicer via web API
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
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!
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!!!
The script is now available here.