text: from tensorflow.keras.preprocessing.image import img_to_array, load_img, array_to_img
  from random import shuffle
  # import PIL
  # import PIL.ImageOps
  # import json
  import numpy as np
  import os
  # import shutil
  import sys
  # import cv2
  
  np.set_printoptions(threshold=sys.maxsize)
  
  import matplotlib.pyplot as plt
  import matplotlib
  
  matplotlib.use('TKAgg')
  from config import *
  
  def plot_results(original_imgs,computed_imgs,size=(100,100)):
      n = len(original_imgs)#10  # how many digits we will display
      plt.figure(figsize=(20, 4))
      for i in range(n):
          # display original
          ax = plt.subplot(2, n, i + 1)
          plt.imshow(original_imgs[i].reshape(size[0], size[1]),cmap='gray_r')
  #         plt.gray()
          ax.get_xaxis().set_visible(False)
          ax.get_yaxis().set_visible(False)
      
          # display reconstruction
          ax = plt.subplot(2, n, i + 1 + n)
          plt.imshow(computed_imgs[i].reshape(size[0], size[1]),cmap='gray_r')
  #         plt.gray()
          ax.get_xaxis().set_visible(False)
          ax.get_yaxis().set_visible(False)
      plt.show()
  
  def read_input_image_pairs(datafolder=INPUT_DATA_FOLDER):
      profile_pngs = []
      midcurve_pngs = []
      for file in os.listdir(datafolder):
          fullpath = os.path.join(datafolder, file)
          if os.path.isdir(fullpath):
              continue
          if file.endswith(".png"):
              if file.find("Profile") != -1:
                  profile_pngs.append(fullpath)
              if file.find("Midcurve") != -1:
                  midcurve_pngs.append(fullpath)
      profile_pngs = sorted(profile_pngs)
      midcurve_pngs = sorted(midcurve_pngs)
      return profile_pngs,midcurve_pngs
  
  def get_training_data(datafolder = INPUT_DATA_FOLDER, size=(100, 100)):
      profile_pngs,midcurve_pngs = read_input_image_pairs(datafolder)
   
   
      profile_pngs_objs = [img_to_array(load_img(f, color_mode='rgba', target_size=size)) for f in profile_pngs ]
      midcurve_pngs_objs = [img_to_array(load_img(f, color_mode='rgba', target_size=size)) for f in midcurve_pngs]
      
  #     profile_pngs_objs = np.array([x.reshape((1,) + x.shape) for x in profile_pngs_objs])
  #     midcurve_pngs_objs = np.array([x.reshape((1,) + x.shape) for x in midcurve_pngs_objs])
  
      profile_pngs_gray_objs = [x[:,:,3] for x in profile_pngs_objs]
      midcurve_pngs_gray_objs =[x[:,:,3] for x in midcurve_pngs_objs]
  
  #     profile_pngs_gray_objs = [np.where(x>128, 0, 1) for x in profile_pngs_gray_objs]
  #     midcurve_pngs_gray_objs =[np.where(x>128, 0, 1) for x in midcurve_pngs_gray_objs]
          
      # shufle them
      zipped_profiles_midcurves = [(p,m) for p,m in zip(profile_pngs_gray_objs,midcurve_pngs_gray_objs)]
      shuffle(zipped_profiles_midcurves)
      profile_pngs_gray_objs, midcurve_pngs_gray_objs = zip(*zipped_profiles_midcurves)
      
      return profile_pngs_gray_objs, midcurve_pngs_gray_objs
  