@@ -442,7 +442,7 @@ def transform(self, trf=None, method='linear', rotation='corner', resample=True,
442
442
443
443
raise ValueError ("Pass \' trf\' as either an Affine or Warp object" )
444
444
445
- def reorient (self , orientation , copy = True ):
445
+ def reorient (self , orientation , copy = True , inplace = False ):
446
446
"""
447
447
Realigns image data and world matrix to conform to a specific slice orientation.
448
448
@@ -452,6 +452,8 @@ def reorient(self, orientation, copy=True):
452
452
Case-insensitive orientation string.
453
453
copy : bool
454
454
Return copy of image even if target orientation is already satisfied.
455
+ inplace : bool
456
+ Reorient the image data in place if it is True.
455
457
456
458
Returns
457
459
-------
@@ -488,8 +490,13 @@ def reorient(self, orientation, copy=True):
488
490
voxsize = voxsize_swapped
489
491
490
492
# initialize new
491
- data = self .data .copy ()
492
- affine = self .geom .vox2world .matrix .copy ()
493
+ if (not inplace ):
494
+ data = self .data .copy ()
495
+ affine = self .geom .vox2world .matrix .copy ()
496
+ else :
497
+ data = self .data
498
+ self .geom .vox2world .matrix .flags .writeable = True
499
+ affine = self .geom .vox2world .matrix
493
500
494
501
# align axes
495
502
affine [:, world_axes_trg ] = affine [:, world_axes_src ]
@@ -507,12 +514,18 @@ def reorient(self, orientation, copy=True):
507
514
affine [:, i ] = - affine [:, i ]
508
515
affine [:3 , 3 ] = affine [:3 , 3 ] - affine [:3 , i ] * (data .shape [i ] - 1 )
509
516
510
- # update geometry
511
- target_geom = ImageGeometry (
512
- shape = data .shape [:3 ],
513
- vox2world = affine ,
514
- voxsize = voxsize )
515
- return self .new (data , target_geom )
517
+ if (not inplace ):
518
+ # update geometry
519
+ target_geom = ImageGeometry (
520
+ shape = data .shape [:3 ],
521
+ vox2world = affine ,
522
+ voxsize = voxsize )
523
+ return self .new (data , target_geom )
524
+ else :
525
+ self .geom .update (voxsize = voxsize , vox2world = affine )
526
+ self .data = data
527
+ return self
528
+
516
529
517
530
def reshape (self , shape , center = 'image' , copy = True ):
518
531
"""
0 commit comments