@gen.coroutine
  def _UpdateImageMaskConfig(self, test_name, image_name, scheme):

    _IMAGE_MASK_CONFIG_PATH = os.path.join(options.options.testing_path, 'config')
    _IMAGE_MASK_CONFIG_FILE = '%s/%s/image_masks_json.cfg' % (_IMAGE_MASK_CONFIG_PATH, scheme)
    _FULL_RESULTS_BASELINE = '%s/results/baseline' % options.options.testing_path
    _FULL_RESULTS_CURRENT = '%s/results/current' % options.options.testing_path
    baseline_image = r'%s/%s/%s/%s' % (_FULL_RESULTS_BASELINE, scheme, test_name, image_name)
    current_image = r'%s/%s/%s/Run 1/%s' % (_FULL_RESULTS_CURRENT, scheme, test_name, image_name)

    from PIL import Image, ImageChops

    try:
      im1 = Image.open(baseline_image)
      im2 = Image.open(current_image)
      _IMAGE_MASK_CONFIG = {}
      diff = ImageChops.difference(im2, im1)
      # TODO:  if image delta is smaller than 100 x 100 write to config
      bbox = diff.getbbox()
      if bbox is not None:
        if os.path.exists(_IMAGE_MASK_CONFIG_FILE):
          config_data = open(_IMAGE_MASK_CONFIG_FILE, 'r')
          _IMAGE_MASK_CONFIG = json.load(config_data)
        width = bbox[2] - bbox[0];
        height = bbox[3] - bbox[1]
        if width <= 100 and height <= 100:
          x, y = 0, 0
          mask = { "x":bbox[0], "y":bbox[1], "height":height-1, "width":width-1 }
          logging.info('bbox: %s' % mask)
          if test_name not in _IMAGE_MASK_CONFIG:
            _IMAGE_MASK_CONFIG[test_name] = {}
            _IMAGE_MASK_CONFIG[test_name][image_name] = []
          else:
            if image_name not in _IMAGE_MASK_CONFIG[test_name]:
              _IMAGE_MASK_CONFIG[test_name][image_name] = []
            else:
              # do not allow duplicate masks
              for dict in _IMAGE_MASK_CONFIG[test_name][image_name]:
                if dict['x'] == bbox[0] and dict['y'] == bbox[1]:
                  if dict['height'] == height or dict['width'] == width:
                    return;

          _IMAGE_MASK_CONFIG[test_name][image_name].append(mask)
          cfg_data_file = open(_IMAGE_MASK_CONFIG_FILE, 'w')
          cfg_data_file.write(json.dumps(_IMAGE_MASK_CONFIG))
          logging.info('Updated %s' % _IMAGE_MASK_CONFIG_FILE)
    except (IOError, ValueError) as e:
      logging.exception('Exception during image comparison')
dataset/ETHPy150Open viewfinderco/viewfinder/backend/www/test_hook.py/TestHookHandler._UpdateImageMaskConfig/original