NVM i have finally done it!
Here is the python code that i had to introduce in the console
import os
import slicer
import vtk
from vtk.util.numpy_support import numpy_to_vtk
Define the output directory
outputDir = r’G:/Kaggle imagini/Extract test din 3d slicer/MRBrainTumor1_Images’
if not os.path.exists(outputDir):
os.makedirs(outputDir)
Load the sample data
import SampleData
sampleDataLogic = SampleData.SampleDataLogic()
mrBrainTumorVolume = sampleDataLogic.downloadSample(‘MRBrainTumor1’)
Get the volume node
volumeNode = slicer.util.getNode(‘MRBrainTumor1’)
Get the volume array
volumeArray = slicer.util.arrayFromVolume(volumeNode)
Save each slice as a JPG file
numSlices = volumeArray.shape[0]
for sliceIndex in range(numSlices):
sliceArray = volumeArray[sliceIndex, :, :]
Flip the array vertically
sliceArray = sliceArray[::-1, :]
Convert the numpy array to a vtkImageData
vtkImageData = vtk.vtkImageData()
vtkImageData.SetDimensions(sliceArray.shape[1], sliceArray.shape[0], 1)
vtkImageData.AllocateScalars(vtk.VTK_UNSIGNED_CHAR, 1)
sliceArrayVtk = numpy_to_vtk(sliceArray.ravel(), deep=True, array_type=vtk.VTK_UNSIGNED_CHAR)
vtkImageData.GetPointData().SetScalars(sliceArrayVtk)
Write the slice to a JPG file
writer = vtk.vtkJPEGWriter()
sliceFilename = os.path.join(outputDir, f’MRBrainTumor1_slice{sliceIndex:03}.jpg’)
writer.SetFileName(sliceFilename)
writer.SetInputData(vtkImageData)
writer.Write()
print(“Export completed.”)
Pe joi, 23 mai 2024 la 19:30:48 EEST, Mike Bindschadler via 3D Slicer Community notifications@slicer.discoursemail.com a scris:
There are many export format options:
3D Slicer is conceptually built around volumetric images rather than single image slices. The main output formats (NRRD, NifTI, etc) contain all the image slices organized in 3D in a single file. So, when you export MRBrainTumor1, you get all the slices and their 3D organization.
–
Previous Replies
Could you please provide some screenshots please? I really struggle to understand how to do it…
(Also i need all the images not just one)
Pe miercuri, 22 mai 2024 la 22:21:55 EEST, Mike Bindschadler via 3D Slicer Community notifications@slicer.discoursemail.com a scris:
When you click on it in SampleData, you download it, so you already have it. If you click “Save”, you can choose what format you want to save the image it in the save scene dialog window. Or, you can go to the Data module, right click on the entry for the image you want, and select “Export to file” to save only the image volume without saving the scene file as well.
–
Previous Replies
Sorry for the late reply just came back home, i’ll go eat something quick and be back
Sure i will send a screenshot, also if you would like we can add each other on discord and i could screenshare my screen (name: Dezmodani)
MRBrainTumor1, the one on the left