# Load image to a model

**URL:** <https://discourse.slicer.org/t/load-image-to-a-model/6237>\
**Category:** Development\
**Created:** [March 21, 2019, 12:56pm UTC](https://discourse.slicer.org/t/load-image-to-a-model/6237 "2019-03-21T12:56:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex1](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@Alex1](https://discourse.slicer.org/u/Alex1)\
**Post date:** [March 21, 2019, 12:56pm UTC](https://discourse.slicer.org/t/load-image-to-a-model/6237/1 "2019-03-21T12:56:03Z")

</div>

Hi all,  
Sorry to bother you with my question. I’m new to Slicer and I’m trying to write a scripted python module. I’m trying to recognize MRI images by a deep-learning model. I have already loaded my model and I’m now trying to load my image to my module. My image is a single image in jpg format. I tried this:

> name=slicer.mrmlScene.GetNodeByID(imagename)  
> img=cv2.imread(name)  
> x = cv2.resize((img), (224, 224), interpolation=cv2.INTER\_CUBIC) / 255.0

it didn’t work, I don’t think it read the image.What is the correct way of implementing this?(by the way, I have already installed cv2)

Thanks,  
Alex

---

<div class="post-metadata">

**Author:** ![adamrankin](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/adamrankin/32/155_2.png) [@adamrankin](https://discourse.slicer.org/u/adamrankin)\
**Post date:** [March 21, 2019, 1:29pm UTC](https://discourse.slicer.org/t/load-image-to-a-model/6237/2 "2019-03-21T13:29:59Z")

</div>

You’ve got a couple options.

Read the file directly from disk (not recommended), using conventional `cv2.imread(<fullPathToFile>)`

If you’ve loaded the image into Slicer, then you can do

```python
import cv2
from vtk.util import numpy_support

imageNode = slicer.util.getNode('<nameOfJpgImageWithoutExtension>')
vtk_image = imageNode.GetImageData()
np_image = numpy_support.vtk_to_numpy(vtk_image.GetPointData().GetScalars())
dims = vtk_image.GetDimensions()
np_image.reshape(dims[0], dims[1], dims[2])

```

See this link [https://stackoverflow.com/questions/12754971/how-to-convert-a-3d-vtkdataset-into-a-numpy-array](https://stackoverflow.com/questions/12754971/how-to-convert-a-3d-vtkdataset-into-a-numpy-array) for more information.

cv2 python bindings use numpy arrays as their images.

---

<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:** [March 21, 2019, 6:19pm UTC](https://discourse.slicer.org/t/load-image-to-a-model/6237/3 "2019-03-21T18:19:42Z")

</div>

You can also load an image directly to a volume node:

```
volumeNode = slicer.util.loadVolume('path/to/myimage.jpg')
```
