def getBoundingBox(nim):
    """Get the bounding box an image.

    The bounding box is the smallest box covering all non-zero elements.

    :Returns:
      tuple(2-tuples) | None
        It returns as many (min, max) tuples as there are image dimensions. The
        order of dimensions is identical to that in the data array. `None` is
        returned of the images does not contain non-zero elements.

    Examples:

      >>> from cnifti import NiftiImage
      >>> nim = NiftiImage(N.zeros((12, 24, 32)))
      >>> nim.bbox is None
      True

      >>> nim.data[3,10,13] = 1
      >>> nim.data[6,20,26] = 1
      >>> nim.bbox
      ((3, 6), (10, 20), (13, 26))

      >>> nim.crop()
      >>> nim.data.shape
      (4, 11, 14)
      >>> nim.bbox
      ((0, 3), (0, 10), (0, 13))

    .. seealso::
      :attr:`nifti.image.NiftiImage.bbox`,
      :func:`nifti.imgfx.crop`
    """
    nz = nim.data.squeeze().nonzero()

    bbox = []

    for dim in nz:
        # check if there are nonzero elements at all
        if not len(dim):
            return None
        bbox.append((dim.min(), dim.max()))

    return tuple(bbox)