# Plot images from PNG files in slicer jupyter kernel

**URL:** https://discourse.slicer.org/t/plot-images-from-png-files-in-slicer-jupyter-kernel/12432
**Category:** Support
**Created:** [July 8, 2020, 1:50am UTC](https://discourse.slicer.org/t/plot-images-from-png-files-in-slicer-jupyter-kernel/12432 "2020-07-08T01:50:56Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![BennetMontgomery](https://avatars.discourse-cdn.com/v4/letter/b/c2a13f/32.png) [@BennetMontgomery](https://discourse.slicer.org/u/BennetMontgomery)
#### Post date: [July 8, 2020, 1:50am UTC](https://discourse.slicer.org/t/plot-images-from-png-files-in-slicer-jupyter-kernel/12432/1 "2020-07-08T01:50:56Z")

</div>

I have a Jupyter Notebook that uses a Slicer kernel and I would like to plot a variable number of captures of the 3D view in the notebook, each view captured from different scenes loaded sequentially. The way I have it set up now is the notebook takes shots of the 3D view and writes them to png files, and I would like to load the png files into the notebook and plot them in a table.

I’ve tried matplotlib and ipyplot, but they don’t work with a slicer kernel in jupyter notebooks. The most potentially useful resource I’ve found is the [Slicer Jupyter](https://github.com/Slicer/SlicerJupyter/) library, but I don’t see any way to use it to load and plot png files. Is anyone aware of a way to plot png files in jupyter notebooks using a slicer kernel?

---

<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: [July 8, 2020, 1:00pm UTC](https://discourse.slicer.org/t/plot-images-from-png-files-in-slicer-jupyter-kernel/12432/2 "2020-07-08T13:00:21Z")

</div>

> [@BennetMontgomery](#):
>
> The way I have it set up now is the notebook takes shots of the 3D view and writes them to png files, and I would like to load the png files into the notebook and plot them in a table.

This is already implemented with a one-liner for slice views:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/4/e/4ebd0806a2c45e8d24622965cfbfd8855bdde06d.jpeg)

By changing a few lines in [ViewLightboxDisplay script](https://github.com/Slicer/SlicerJupyter/blob/beafcb192bc44ccc88e0c2c478df89fefcd43537/JupyterNotebooks/JupyterNotebooksLib/display.py#L236-L283) (essentially, replace `screenCaptureLogic.captureSliceSweep` by [`screenCaptureLogic.capture3dViewRotation`](https://github.com/Slicer/Slicer/blob/0bc7cb16bd6cb9cdad7e589f44442e9bf7d0ee79/Modules/Scripted/ScreenCapture/ScreenCapture.py#L1214-L1215), you can create a table showing 3D view in different orientations. I’ve created this using Screen Capture module GUI, you can play a bit with the GUI, too to get to know a bit better what the module can do:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/4/4/443f476b175b5dd20cfc4bec01878f6563d4f4a1.jpeg)

Finally, here is an example of a script creating some data augmentation for training data and generation of an overview image of all data sets:

> [@Generating augmented training data from nrrd file using random translations, rotations, and deformations](https://discourse.slicer.org/t/generating-augmented-training-data-from-nrrd-file-using-random-translations-rotations-and-deformations/11810/8):
>
> I’ve updated the volume augmentation script to apply random translations and rotations in addition to the random deformations, and to also take screenshots so that you can get a quick overview of how the augmented data looks like. Gallery of example output: Script that generated it:

---

<div class="post-metadata">

### Author: ![BennetMontgomery](https://avatars.discourse-cdn.com/v4/letter/b/c2a13f/32.png) [@BennetMontgomery](https://discourse.slicer.org/u/BennetMontgomery)
#### Post date: [July 8, 2020, 11:02pm UTC](https://discourse.slicer.org/t/plot-images-from-png-files-in-slicer-jupyter-kernel/12432/3 "2020-07-08T23:02:11Z")

</div>

Thanks for the help but unfortunately, this doesn’t solve my problem. Let me just clarify what I’m trying to accomplish:

I’m loading mrb scenes into the jupyter managed slicer, constructing a volume, rendering the volume, and then capturing a screenshot of the 3D view for display. The number of scenes is variable and I would like for the user of the notebook to load as many scenes as they like. At present, I have a cell that allows the user to input paths to mrb scenes to load into the notebook:

 ![Screenshot_20200708_165321](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/c/f/cfc512ff2eb3a2d6e413a253f639bdc74bc461be.png)

Later on, I use a for loop to load one of the scenes, do the processing, capture the 3d view, and repeat until all scenes have been processed:

 ![Screenshot_20200708_172201](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/d/0d9bb93c09d58c0efa88f652e616e439f465d7d7.png)

In that cell I try using the lightbox display, but neither it nor the view3ddisplay() method seem to work when embedded in for loops as both give an identical result:

 ![Screenshot_20200708_172314](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/a/8a4bb4861c9ac69d413a87be744698aed55f6114.png)

Everything works fine but no images are displayed in the notebook. The view3ddisplay() method works when not embedded in a for loop but I don’t see a way to retain a theoretically limitless number of scenes to test on without it. As I see it, my only option is to get rid of the for loop and abandon being able to load a limitless number of scenes but I would really like to avoid that if at all possible.

Thanks!

---

<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: [July 9, 2020, 12:49am UTC](https://discourse.slicer.org/t/plot-images-from-png-files-in-slicer-jupyter-kernel/12432/4 "2020-07-09T00:49:15Z")

</div>

Jupyter notebook shows only the last result. If you want to show multiple results then you need to put each result that you want to display as input argument of `display()` function.

For example, if you want to display 3 slice views in a for loop:

This only displays the last one:

```python
for layout in ["OneUpRedSlice", "OneUpYellowSlice", "OneUpGreenSlice"]:
    slicernb.ViewDisplay(layout) # wrong!

```

This shows all 3 views:

```python
for layout in ["OneUpRedSlice", "OneUpYellowSlice", "OneUpGreenSlice"]:
    display(slicernb.ViewDisplay(layout))

```

---

<div class="post-metadata">

### Author: ![BennetMontgomery](https://avatars.discourse-cdn.com/v4/letter/b/c2a13f/32.png) [@BennetMontgomery](https://discourse.slicer.org/u/BennetMontgomery)
#### Post date: [July 9, 2020, 2:50am UTC](https://discourse.slicer.org/t/plot-images-from-png-files-in-slicer-jupyter-kernel/12432/5 "2020-07-09T02:50:52Z")

</div>

Thanks, that works perfectly!
