old_contents: from PIL import Image
  import numpy as np
  import matplotlib.pyplot as plt
  import math
  
  
  def distance_transformation(path):
      '''
  
      :param path:
      :return:
  
  
      SAMPLE MATLAB CODE
      function [distance] = DistanceTransform(image)
  [sizeY, sizeX] = size(image);
  distance(1:sizeY,1:sizeX) = 255;
  dL =  zeros(1,4);
  dR = zeros(1,4);
  
  for i = 1:sizeY
      for j = 1:sizeX
          if (image(i,j) == 1)
              distance(i,j)=0;
          end
      end
  end
  
  for i = 2:sizeY
      for j = 2:sizeX-1
          if (distance(i,j)>0)
              dL(1) = 2 + distance(i-1,j-1);
              dL(2) = 1 + distance(i-1,j);
              dL(3) = 2 + distance(i-1,j+1);
              dL(4) = 1 + distance(i,j-1);
              distance(i,j) = min(dL);
          end
      end
  end
  for i = sizeY-1:1
      for j = sizeX-1:2
          if(distance(i,j)>0)
              dR(1) = 1 + distance(i,j+1);
              dR(2) = 2 + distance(i+1,j-1);
              dR(3) = 1 + distance(i+1,j);
              dR(4) = 2 + distance(i+1,j+1);
              distance(i,j) = min(dL);
          end
      end
  end
  distance = uint8(distance);
  end
      '''
new_contents: from PIL import Image
  import numpy as np
  import matplotlib.pyplot as plt
  import math
  
  
  def distance_transformation(path):
      """
      :param path:
      :return:
      SAMPLE MATLAB CODE
      """
      try:
          i = Image.open(path)
          array = np.asarray(i, dtype=np.float32)
      except TypeError:
          return "Parameters seem not to be right"
      except OSError:
          return "This isn't an image"
      size_x, size_y = array.shape
      distance = (255*np.ones(size_x*size_y)).reshape(size_x, size_y)
      dL = np.zeros(4)
      dR = np.zeros(4)
      for i in range(1, size_x):
          for j in range(1, size_y):
              if array[i, j] == 1:
                  distance[i, j] = 0
      for i in range(2, size_y):
          for j in range(2, size_x-1):
              if distance[i, j] > 0:
                  dL[0] = 2 + distance[i-1, j-1]
                  dL[1] = 1 + distance[i-1, j]
                  dL[2] = 2 + distance[i-1, j+1]
                  dL[3] = 1 + distance[i, j-1]
                  distance[i, j] = min(dL)
      for i in range(size_y-1, 1):
          for j in range(size_x-1, 2):
              if distance[i, j] > 0:
                  dR[0] = 2 + distance[i-1, j-1]
                  dR[1] = 1 + distance[i-1, j]
                  dR[2] = 2 + distance[i-1, j+1]
                  dR[3] = 1 + distance[i, j-1]
                  distance[i, j] = min(dR)
      return distance
  
  if __name__ == "__main__":
      distance_img = distance_transformation('lena.png')
      if type(distance_img) != str:
          plt.imshow(distance_img, cmap='gray', interpolation='nearest')
          plt.show()
      else:
          print(distance_img)
  
subject: Update distance_transformation.py