@@ -515,16 +515,16 @@ class ReorientImage(SimpleInterface):
515
515
output_spec = ReorientImageOutputSpec
516
516
517
517
def _run_interface (self , runtime ):
518
- self ._results ["out_file" ] = reorient_image (
518
+ self ._results ["out_file" ] = reorient_file (
519
519
self .inputs .in_file ,
520
520
target_file = self .inputs .target_file ,
521
521
target_ornt = self .inputs .target_orientation ,
522
522
)
523
523
return runtime
524
524
525
525
526
- def reorient_image (
527
- in_file : str , * , target_file : str = None , target_ornt : str = None , newpath : str = None
526
+ def reorient_file (
527
+ in_file : str , * , target_file : str = None , target_ornt : str = None , newpath : str = None ,
528
528
) -> str :
529
529
"""
530
530
Reorient an image.
@@ -541,16 +541,14 @@ def reorient_image(
541
541
import nibabel as nb
542
542
543
543
img = nb .load (in_file )
544
- img_axcodes = nb . aff2axcodes ( img . affine )
545
- in_ornt = nb . orientations . axcodes2ornt ( img_axcodes )
544
+ if not target_file and not target_ornt :
545
+ raise TypeError ( "No target orientation or file is specified." )
546
546
547
547
if target_file :
548
548
target_img = nb .load (target_file )
549
549
target_ornt = nb .aff2axcodes (target_img .affine )
550
550
551
- out_ornt = nb .orientations .axcodes2ornt (target_ornt )
552
- ornt_xfm = nb .orientations .ornt_transform (in_ornt , out_ornt )
553
- reoriented = img .as_reoriented (ornt_xfm )
551
+ reoriented = reorient_image (img , target_ornt )
554
552
555
553
if newpath is None :
556
554
newpath = Path ()
@@ -559,6 +557,18 @@ def reorient_image(
559
557
return out_file
560
558
561
559
560
+ def reorient_image (img : nb .spatialimages .SpatialImage , target_ornt : str ):
561
+ """Reorient an image in memory."""
562
+ import nibabel as nb
563
+
564
+ img_axcodes = nb .aff2axcodes (img .affine )
565
+ in_ornt = nb .orientations .axcodes2ornt (img_axcodes )
566
+ out_ornt = nb .orientations .axcodes2ornt (target_ornt )
567
+ ornt_xfm = nb .orientations .ornt_transform (in_ornt , out_ornt )
568
+ r_img = img .as_reoriented (ornt_xfm )
569
+ return r_img
570
+
571
+
562
572
def _gen_reference (
563
573
fixed_image ,
564
574
moving_image ,
0 commit comments