code: def main():
      # parse cmd arguments
      parser = getParser()
      parser.parse_args()
      args = getArguments(parser)
  
      # prepare logger
      logger = Logger.getInstance()
      if args.debug:
          logger.setLevel(logging.DEBUG)
      elif args.verbose:
          logger.setLevel(logging.INFO)
  
      # load mask
      logger.info("Loading mask {}...".format(args.mask))
      mask_image, _ = load(args.mask)
  
      # store mask images shape for later check against the input image
      mask_image_shape = mask_image.shape
  
      # extract the position of the foreground object in the mask image
      logger.info("Extract the position of the foreground object...")
      positions = mask_image.nonzero()
      positions = [
          (
              max(0, positions[i].min() - args.offset),
              positions[i].max() + 1 + args.offset,
          )
          for i in range(len(positions))
      ]  # crop negative values
      logger.debug("Extracted position is {}.".format(positions))
  
      # load image
      logger.info("Loading image {}...".format(args.image))
      image_data, image_header = load(args.image)
  
      # check if the mask image and the input image are of the same shape
      if mask_image_shape != image_data.shape:
          raise ArgumentError(
              "The two input images are of different shape (mask: {} and image: {}).".format(
                  mask_image_shape, image_data.shape
              )
          )
  
      # execute extraction of the sub-area
      logger.info("Extracting sub-volume...")
      index = tuple([slice(x[0], x[1]) for x in positions])
      volume = image_data[index]
  
      # check if the output image contains data
      if 0 == len(volume):
          logger.exception(
              "The extracted sub-volume is of zero-size. This usual means that the mask image contained no foreground object."
          )
          sys.exit(0)
  
      logger.debug(
          "Extracted volume is of shape {}.".format(volume.shape)
      )
  
      # get base origin of the image
      origin_base = numpy.array(
          [0] * image_data.ndim
      )  # for backwards compatibility
  
      # modify the volume offset to imitate numpy behavior (e.g. wrap negative values)
      offset = numpy.array([x[0] for x in positions])
      for i in range(0, len(offset)):
          if None == offset[i]:
              offset[i] = 0
      offset[offset < 0] += numpy.array(image_data.shape)[
          offset < 0
      ]  # wrap around
      offset[offset < 0] = 0  # set negative to zero
  
      # calculate final new origin
      origin = origin_base + offset
  
      logger.debug(
          "Final origin created as {} + {} = {}.".format(
              origin_base, offset, origin
          )
      )
  
      # save results in same format as input image
      logger.info("Saving extracted volume...")
      save(volume, args.output, image_header, args.force)
  
      logger.info("Successfully terminated.")
  
file_path: medpy/bin/medpy_extract_sub_volume_by_example.py
imports: from argparse import RawTextHelpFormatter
  import argparse
  import logging
  import sys
  import os
  import numpy
  from medpy.core import ArgumentError, Logger
  from medpy.io import load, save
label_desc: Use image node_id 1 for calling a global function with example usage: main() without return types