Exporting jpgs of MRI slices

I am looking to do the following… I can write the Python code, but I am new to the 3d-slicer API and just need some guidance…
I want to:

  1. Load up a folder(s) of nii image files into 3d-slicer… I’ve done this part and since they are loaded into a listbox I assume(?) there is a reference to this.
  2. Iterate over this list of files.
  3. For each nii file, select either the sagittal, coronal or transverse view.
  4. Then for the selected view, cycle through each slice for that view and export each slice as a jpg/png file.

The goal of this is to extract these to be used as images to train a machine learning model…

I’ve looked over the API and there are SO many methods! if anyone has done this before as a module or can point out the relevant API calls, it would be greatly appreciated!

Thank you!
Harold Chattaway

First, we understand that some machine learning examples are hard-coded to handle only jpg or png, but converting the original data to an 8 bit format truncates the dynamic range and is probably a bad idea.

That out of the way, you can use the ScreenCapture module’s slice sweep mode to catpure the images. Here’s an example doing this from a python script.

The batch processing examples will get you started applying this to a set of images.

This looks like it will be a great help, thanks… I am not using anything hard coded for just jpgs etc… I am writing the app using YOLO/PyTorch so I have control over the image format…

I figured something like this had to have been done before…
Thank you!