Get a flipped and rotated segmentation label image using python script

Hi, there, I recently use python script to export segmentations to labelmap and save it on the disk, to save some clicks. But instead, I get a rotated and flipped labelmap, like below:

9

This should be a “4”, but instead, it was flipped and rotated “4”. I’ve tried both to save labelmap in nii.gz and nrrd format, all gives me the same result.

Here is my code:

def extract_label(mrml_fp):
   # load mrml file
   slicer.util.loadScene(mrml_fp)

    # access manually annotated label
   segs = slicer.util.getNodesByClass('vtkMRMLSegmentationNode')

   # master volume
   volume_node = slicer.util.getNodesByClass('vtkMRMLScalarVolumeNode')

   if len(segs) != 1:
       raise "the number of segmentation node shoule be 1"

   annoted_segment_node = segs[0]
   annoted_seg = annoted_segment_node.GetSegmentation()

   # iterate through all the segments
   num_segments = annoted_seg.GetNumberOfSegments()

   for idx in range(num_segments):

      # create new segmentation node
      new_segement_node = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLSegmentationNode')
      new_segement_node.CreateDefaultDisplayNodes()
   new_segement_node.SetReferenceImageGeometryParameterFromVolumeNode(volume_node[0])

      # add segment to new segmentation node
      segment = annoted_seg.GetNthSegment(idx)
      segment_name = segment.GetName()
      new_segement_node.GetSegmentation().AddSegment(segment)

      # export segmentation node to labelmap
      labelmap_volume_node = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode')
      slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(new_segement_node, labelmap_volume_node, volume_node[0])

      # save labelmap to disk
      dest_folder = os.path.dirname(mrml_fp)
      slicer.util.saveNode(labelmap_volume_node, os.path.join(dest_folder, "{}.nrrd".format(segment_name)))

      # remove created segmentation node
      slicer.mrmlScene.RemoveNode(labelmap_volume_node)
      slicer.mrmlScene.RemoveNode(new_segement_node)

   # clear scene
   slicer.mrmlScene.Clear(0)

Could you please tell me where I went wrong?