text: import numpy as np
  import torch
  from PIL import Image
  
  
  def load_2d_image(img_path, resize_dim=0, type='RGB'):
      image = Image.open(img_path)
      if type == 'RGB':
          image = image.convert(type)
      if resize_dim != 0:
          image = image.resize(resize_dim)
      pix = np.array(image)
      return pix
  
  
  def normalize_intensity(img_tensor, normalization="full_volume_mean", norm_values=(0, 1, 1, 0)):
      """
      Accepts an image tensor and normalizes it
      :param normalization: choices = "max", "mean" , type=str
      """
      if normalization == "mean":
          mask = img_tensor.ne(0.0)
          desired = img_tensor[mask]
          mean_val, std_val = desired.mean(), desired.std()
          img_tensor = (img_tensor - mean_val) / std_val
      elif normalization == "max":
          max_val, _ = torch.max(img_tensor)
          img_tensor = img_tensor / max_val
      elif normalization == 'brats':
          # print(norm_values)
          normalized_tensor = (img_tensor.clone() - norm_values[0]) / norm_values[1]
          final_tensor = torch.where(img_tensor == 0., img_tensor, normalized_tensor)
          final_tensor = 100.0 * ((final_tensor.clone() - norm_values[3]) / (norm_values[2] - norm_values[3])) + 10.0
          x = torch.where(img_tensor == 0., img_tensor, final_tensor)
          return x
  
      elif normalization == 'full_volume_mean':
          img_tensor = (img_tensor.clone() - norm_values[0]) / norm_values[1]
  
      elif normalization == 'max_min':
          img_tensor = (img_tensor - norm_values[3]) / ((norm_values[2] - norm_values[3]))
  
      elif normalization == None:
          img_tensor = img_tensor
      return img_tensor
  
  
  
  def clip_range(img_numpy):
      """
      Cut off outliers that are related to detected black in the image (the air area)
      """
      # Todo median value!
      zero_value = (img_numpy[0, 0, 0] + img_numpy[-1, 0, 0] + img_numpy[0, -1, 0] + \
                    img_numpy[0, 0, -1] + img_numpy[-1, -1, -1] + img_numpy[-1, -1, 0] \
                    + img_numpy[0, -1, -1] + img_numpy[-1, 0, -1]) / 8.0
      non_zeros_idx = np.where(img_numpy >= zero_value)
      [max_z, max_h, max_w] = np.max(np.array(non_zeros_idx), axis=1)
      [min_z, min_h, min_w] = np.min(np.array(non_zeros_idx), axis=1)
      y = img_numpy[min_z:max_z, min_h:max_h, min_w:max_w]
      return y
  
  
  def percentile_clip(img_numpy, min_val=0.1, max_val=99.8):
      """
      Intensity normalization based on percentile
      Clips the range based on the quarile values.
      :param min_val: should be in the range [0,100]
      :param max_val: should be in the range [0,100]
      :return: intesity normalized image
      """
      low = np.percentile(img_numpy, min_val)
      high = np.percentile(img_numpy, max_val)
  
      img_numpy[img_numpy < low] = low
      img_numpy[img_numpy > high] = high
      return img_numpy
  