from typing import Iterable, List from funcy import rpartial from image_prediction.image_extractor.extractor import ImageMetadataPair from image_prediction.stitching.merging import merge_along_both_axes, make_merger_sentinel from image_prediction.utils.generic import until def stitch_pairs(pairs: Iterable[ImageMetadataPair], tolerance=0) -> List[ImageMetadataPair]: """Given a collection of image-metadata pairs from the same pages, combines all pairs that constitute adjacent images.""" no_new_mergers = make_merger_sentinel() merge = rpartial(merge_along_both_axes, tolerance) return until(no_new_mergers, merge, pairs)