# Reorienting Images

**URL:** <https://discourse.itk.org/t/reorienting-images/7189>\
**Category:** Algorithms\
**Tags:** simpleitk, python\
**Created:** [August 22, 2024, 8:18pm UTC](https://discourse.itk.org/t/reorienting-images/7189 "2024-08-22T20:18:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dlevy-ph](https://discourse.itk.org/user_avatar/discourse.itk.org/dlevy-ph/32/4344_2.png) [@dlevy-ph](https://discourse.itk.org/u/dlevy-ph)\
**Post date:** [August 22, 2024, 8:18pm UTC](https://discourse.itk.org/t/reorienting-images/7189/1 "2024-08-22T20:18:13Z")

</div>

Hello, I am trying to achieve the attached process (which I had been doing using `nibabel`) with SimpleITK. I’m not sure how to interpret the cosine matrix, though, so I’m unclear about how to ensure L-P-S orientation:

```auto
DESIRED_ORIENTATION = ('L', 'P', 'S')

def extract_orientation(ctx):
    ctx.affine = ctx.mos_img.affine
    ctx.orientation = nib.aff2axcodes(ctx.affine)

def reorient_mask(ctx, desired_orientation):
    ctx.orig_orientation = ctx.orientation

    # Convert the orientations to the orientation matrices
    current_ornt = nib.orientations.axcodes2ornt(ctx.orientation)
    desired_ornt = nib.orientations.axcodes2ornt(desired_orientation)
    
    # Get the transform from current to desired orientation
    ctx.transform_ornt = nib.orientations.ornt_transform(current_ornt, desired_ornt)
    
    # Apply the transform
    ctx.orig_mos_data = ctx.mos_data
    ctx.mos_data = nib.apply_orientation(ctx.mos_data, ctx.transform_ornt)

    # Update the affine matrix
    ctx.orig_affine = ctx.affine
    ctx.affine = ctx.affine @ nib.orientations.inv_ornt_aff(ctx.transform_ornt, ctx.mos_img.shape)

    # Reorient the image
    ctx.orig_img = ctx.mos_img
    ctx.mos_img = nib.Nifti1Image(ctx.mos_data, ctx.affine)

    # Update the orientation in the context object
    extract_orientation(ctx)

```

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [August 22, 2024, 8:33pm UTC](https://discourse.itk.org/t/reorienting-images/7189/2 "2024-08-22T20:33:53Z")

</div>

Hello @dlevy-ph,

The class you are looking for is called [DICOMOrientImageFilter](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1DICOMOrientImageFilter.html), so this should work for you `sitk.DICOMOrient(image, "LPS")`.

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [August 22, 2024, 9:05pm UTC](https://discourse.itk.org/t/reorienting-images/7189/3 "2024-08-22T21:05:57Z")

</div>

> [@zivy](#):
>
> “LPS”

To clarify, with the DICOMOrientImageFilter the notation is the “To” direction for the label of axes. That is LPS means { right-to-Left, anterior-to-Posterior, inferior-to-Superior}.
