def test_resize_object(self): obj = { "x0": 5, "x1": 10, "top": 20, "bottom": 30, "width": 5, "height": 10, "doctop": 120, "y0": 40, "y1": 50, } assert utils.resize_object(obj, "x0", 0) == { "x0": 0, "x1": 10, "top": 20, "doctop": 120, "bottom": 30, "width": 10, "height": 10, "y0": 40, "y1": 50, } assert utils.resize_object(obj, "x1", 50) == { "x0": 5, "x1": 50, "top": 20, "doctop": 120, "bottom": 30, "width": 45, "height": 10, "y0": 40, "y1": 50, } assert utils.resize_object(obj, "top", 0) == { "x0": 5, "x1": 10, "top": 0, "doctop": 100, "bottom": 30, "height": 30, "width": 5, "y0": 40, "y1": 70, } assert utils.resize_object(obj, "bottom", 40) == { "x0": 5, "x1": 10, "top": 20, "doctop": 120, "bottom": 40, "height": 20, "width": 5, "y0": 30, "y1": 50, }
def join_edge_group(edges, orientation, tolerance=DEFAULT_JOIN_TOLERANCE): """ Given a list of edges along the same infinite line, join those that are within `tolerance` pixels of one another. """ if orientation == "h": min_prop, max_prop = "x0", "x1" elif orientation == "v": min_prop, max_prop = "top", "bottom" else: raise ValueError("Orientation must be 'v' or 'h'") sorted_edges = list(sorted(edges, key=itemgetter(min_prop))) joined = [ sorted_edges[0] ] for e in sorted_edges[1:]: last = joined[-1] if e[min_prop] <= (last[max_prop] + tolerance): if e[max_prop] > last[max_prop]: # Extend current edge to new extremity joined[-1] = utils.resize_object(last, max_prop, e[max_prop]) else: # Do nothing; edge is fully contained in pre-existing edge pass else: # Edge is separate from previous edges joined.append(e) return joined