def _distributeCenter(self, index, rects, selectedContours, selectedBPoints): ordered = [(bPoint.anchor[index], (bPoint.anchor[0], bPoint.anchor[1], bPoint.anchor[0], bPoint.anchor[1]), bPoint) for bPoint in selectedBPoints] ordered += [(contour.bounds[index], contour.bounds, contour) for contour in selectedContours] ordered.sort() side1 = ordered[0] side2 = ordered[-1] center1 = rectCenter(side1[1])[index] center2 = rectCenter(side2[1])[index] space = center2 - center1 if not space: return step = space / (len(ordered) - 1) prev = center1 for pos, bounds, obj in ordered[1:-1]: alignTo = prev + step d = alignTo - rectCenter(bounds)[index] if index == 0: obj.moveBy((d, 0)) else: obj.moveBy((0, d)) prev = alignTo
def _alignXCenter(self, rects, selectedContours, selectedBPoints): x1 = getEdgeCoordinate(rects, 0, min) x2 = getEdgeCoordinate(rects, 2, max) center = (x1 + x2) / 2 for bPoint in selectedBPoints: d = center - bPoint.anchor[0] bPoint.moveBy((d, 0)) for contour in selectedContours: d = center - rectCenter(contour.bounds)[0] contour.moveBy((d, 0))
def _alignYCenter(self, rects, selectedContours, selectedBPoints): y1 = getEdgeCoordinate(rects, 1, min) y2 = getEdgeCoordinate(rects, 3, max) center = (y1 + y2) / 2 for bPoint in selectedBPoints: d = center - bPoint.anchor[1] bPoint.moveBy((0, d)) for contour in selectedContours: d = center - rectCenter(contour.bounds)[1] contour.moveBy((0, d))
def test_rectCenter(): assert rectCenter((0, 0, 100, 200)) == (50.0, 100.0) assert rectCenter((0, 0, 100, 199.0)) == (50.0, 99.5)