# Programmatically load transform apply to image and then save

**URL:** <https://discourse.slicer.org/t/programmatically-load-transform-apply-to-image-and-then-save/2505>\
**Category:** Support\
**Created:** [April 3, 2018, 6:48pm UTC](https://discourse.slicer.org/t/programmatically-load-transform-apply-to-image-and-then-save/2505 "2018-04-03T18:48:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kayarre](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kayarre/32/3606_2.png) [@kayarre](https://discourse.slicer.org/u/kayarre)\
**Post date:** [April 3, 2018, 6:48pm UTC](https://discourse.slicer.org/t/programmatically-load-transform-apply-to-image-and-then-save/2505/1 "2018-04-03T18:48:30Z")

</div>

I am trying to automate parts of my workflow in Slicer, ( my mental model wants something like VMTK or the CLI where I can break the operations into smaller steps and add and rearrange as needed)

I have figured out how to read in a .h5 transform and and image volume with python commands but it is not clear how to apply the transform to the image volume and save or “harden” transformed image to be subsequently processed with Brainsfit.

i have been reading through  
[[https://www.slicer.org/wiki/Documentation/Nightly/Modules/Transforms](https://www.slicer.org/wiki/Documentation/Nightly/Modules/Transforms)] (Transforms docs)

but it is more confusing than helpful

---

<div class="post-metadata">

**Author:** ![kayarre](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kayarre/32/3606_2.png) [@kayarre](https://discourse.slicer.org/u/kayarre)\
**Post date:** [April 3, 2018, 7:01pm UTC](https://discourse.slicer.org/t/programmatically-load-transform-apply-to-image-and-then-save/2505/2 "2018-04-03T19:01:13Z")

</div>

This worked for me:

[success, dsa] = slicer.util.loadVolume(r’./image\_vols/DSA.nrrd’, returnNode=True)  
#relative paths seem to not work with transforms, filed on a bug on that.  
[success, dss2post\_trans] = slicer.util.loadTransform(r’/home/sansomk/caseFiles/mri/VWI\_proj/case4/registration/dsa2post\_man\_LT.h5’, returnNode=True)  
dsa.SetAndObserveTransformNodeID(dsa2post\_trans.GetID())  
slicer.vtkSlicerTransformLogic().hardenTransform(dsa)  
slicer.util.saveNode(dsa, “./registration/dsa2post\_man\_trans.nrrd”)

I haven’t tried this outside of the python interactor, but ideally I would be able to execute this without the slicer gui, I assume this is possible but I am don’t know how.

---

<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:** [April 3, 2018, 10:13pm UTC](https://discourse.slicer.org/t/programmatically-load-transform-apply-to-image-and-then-save/2505/3 "2018-04-03T22:13:40Z")

</div>

This post should help: [Slicer batch processing question (--no-main-window --python-script)](https://discourse.slicer.org/t/slicer-batch-processing-question-no-main-window-python-script/1863)

If you still have questions then let us know.
