def get_diffed_response_codes(cur_rc: Image, list_dir: str) -> Tuple[Image, Image]: cur_rc = cur_rc.threshold() # Load the reference response codes, bold and not bold version. ref_rc = Image.from_file(list_dir + utils.RESPONSE_CODES_IMAGE_FILENAME, Rotation.NONE) ref_rc = ref_rc.threshold() bold_ref_rc = Image.from_file( list_dir + utils.BOLD_RESPONSE_CODES_IMAGE_FILENAME, Rotation.NONE) bold_ref_rc = bold_ref_rc.threshold(bold_ref_rc) # Align and diff against both reference images. aligned_rc = cur_rc.align_to(ref_rc) bold_aligned_rc = cur_rc.align_to(bold_ref_rc) diff = aligned_rc.diff_against(ref_rc) bold_diff = bold_aligned_rc.diff_against(bold_ref_rc) # Count how many white pixels are in each diff. white_pixels = diff.numWhitePixels() bold_white_pixels = bold_diff.numWhitePixels() # The one with the least white pixels should be the correct image. if white_pixels < bold_white_pixels: return diff, aligned_rc else: return bold_diff, bold_aligned_rc