# Exporting jpgs of MRI slices

**URL:** https://discourse.slicer.org/t/exporting-jpgs-of-mri-slices/28057
**Category:** Development
**Tags:** python
**Created:** [February 27, 2023, 3:34am UTC](https://discourse.slicer.org/t/exporting-jpgs-of-mri-slices/28057 "2023-02-27T03:34:43Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![hchattaway](https://avatars.discourse-cdn.com/v4/letter/h/43a26b/32.png) [@hchattaway](https://discourse.slicer.org/u/hchattaway)
#### Post date: [February 27, 2023, 3:34am UTC](https://discourse.slicer.org/t/exporting-jpgs-of-mri-slices/28057/1 "2023-02-27T03:34:43Z")

</div>

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

---

<div class="post-metadata">

### Author: ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)
#### Post date: [February 27, 2023, 8:39pm UTC](https://discourse.slicer.org/t/exporting-jpgs-of-mri-slices/28057/2 "2023-02-27T20:39:25Z")

</div>

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](https://slicer.readthedocs.io/en/latest/user_guide/modules/screencapture.html) module’s slice sweep mode to catpure the images. [Here’s an example](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#capture-a-slice-view-sweep-into-a-series-of-png-files) doing this from a python script.

The [batch processing](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#batch-processing) examples will get you started applying this to a set of images.

---

<div class="post-metadata">

### Author: ![hchattaway](https://avatars.discourse-cdn.com/v4/letter/h/43a26b/32.png) [@hchattaway](https://discourse.slicer.org/u/hchattaway)
#### Post date: [February 28, 2023, 3:54pm UTC](https://discourse.slicer.org/t/exporting-jpgs-of-mri-slices/28057/3 "2023-02-28T15:54:51Z")

</div>

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!  
Harold
