code: def crop_image(image, crop_mask, crop_internal=False):
      """Crop an image to the size of the nonzero portion of a crop mask"""
      i_histogram = crop_mask.sum(axis=1)
      i_cumsum = numpy.cumsum(i_histogram != 0)
      j_histogram = crop_mask.sum(axis=0)
      j_cumsum = numpy.cumsum(j_histogram != 0)
      if i_cumsum[-1] == 0:
          # The whole image is cropped away
          return numpy.zeros((0, 0), dtype=image.dtype)
      if crop_internal:
          #
          # Make up sequences of rows and columns to keep
          #
          i_keep = numpy.argwhere(i_histogram > 0)
          j_keep = numpy.argwhere(j_histogram > 0)
          #
          # Then slice the array by I, then by J to get what's not blank
          #
          return image[i_keep.flatten(), :][:, j_keep.flatten()].copy()
      else:
          #
          # The first non-blank row and column are where the cumsum is 1
          # The last are at the first where the cumsum is it's max (meaning
          # what came after was all zeros and added nothing)
          #
          i_first = numpy.argwhere(i_cumsum == 1)[0]
          i_last = numpy.argwhere(i_cumsum == i_cumsum.max())[0]
          i_end = i_last + 1
          j_first = numpy.argwhere(j_cumsum == 1)[0]
          j_last = numpy.argwhere(j_cumsum == j_cumsum.max())[0]
          j_end = j_last + 1
  
          if image.ndim == 3:
              return image[
                  i_first[0] : i_end[0], j_first[0] : j_end[0], :
              ].copy()
  
          return image[
              i_first[0] : i_end[0], j_first[0] : j_end[0]
          ].copy()
  
imports: import logging
  import os
  import re
  import shutil
  import sys
  import tempfile
  import urllib.request
  from urllib.parse import parse_qs, unquote, urlparse
  from urllib.request import urlopen
  import boto3
  import numpy
  import pkg_resources
  import scipy.io
  from google.cloud import storage
  from google.cloud.exceptions import NotFound
  from google.auth.exceptions import DefaultCredentialsError
  from ..constants.image import ALL_IMAGE_EXTENSIONS, FILE_SCHEME, PASSTHROUGH_SCHEMES, SUPPORTED_MOVIE_EXTENSIONS
  from ..constants.measurement import FTR_WELL
  from .measurement import is_well_column_token, is_well_row_token
label_desc: Use image node_id 13 for calling a global function with example usage: crop_image(image, crop_mask, crop_internal) and returns: numpy, image, image, image