code: def get_crop_region(mask, pad=0):
      """finds a rectangular region that contains all masked ares in an image. Returns (x1, y1, x2, y2) coordinates of the rectangle.
      For example, if a user has painted the top-right part of a 512x512 image", the result may be (256, 0, 512, 256)
      """
  
      h, w = mask.shape
  
      crop_left = 0
      for i in range(w):
          if not (mask[:, i] == 0).all():
              break
          crop_left += 1
  
      crop_right = 0
      for i in reversed(range(w)):
          if not (mask[:, i] == 0).all():
              break
          crop_right += 1
  
      crop_top = 0
      for i in range(h):
          if not (mask[i] == 0).all():
              break
          crop_top += 1
  
      crop_bottom = 0
      for i in reversed(range(h)):
          if not (mask[i] == 0).all():
              break
          crop_bottom += 1
  
      return (
          int(max(crop_left - pad, 0)),
          int(max(crop_top - pad, 0)),
          int(min(w - crop_right + pad, w)),
          int(min(h - crop_bottom + pad, h)),
      )
  
imports: from PIL import Image, ImageFilter, ImageOps
label_desc: Use image node_id 1 for calling a global function with example usage: get_crop_region(mask, pad) and returns: int,  int,  int,  int