def test_intersect_no_interesction(self): bounding_box = BoundingBox(5, 5, 15, 15) bounding_box_to_intersect = BoundingBox(-10, 0, 4, 4) self.assertEqual(bounding_box.intersect(bounding_box_to_intersect), None) bounding_box_to_intersect = BoundingBox(16, 16, 17, 17) self.assertEqual(bounding_box.intersect(bounding_box_to_intersect), None)
def _pasteImage(self, pasted_image, pasted_image_mask, image_background, x_offset, y_offset): pasted_image_info = ImageInfo(pasted_image) image_background_info = ImageInfo(image_background) image_background_bbox = BoundingBox( top=0, left=0, bottom=image_background_info.height, right=image_background_info.width) pasted_image_bbox = BoundingBox( top=y_offset, left=x_offset, bottom=y_offset + pasted_image_info.height, right=x_offset + pasted_image_info.width) intersection_bbox = pasted_image_bbox.intersect(image_background_bbox) pasted_image_y_offset_from, pasted_image_x_offset_from, pasted_image_y_offset_to, pasted_image_x_offset_to = 0, 0, 0, 0 if image_background_bbox.left > pasted_image_bbox.left: pasted_image_x_offset_from = -pasted_image_bbox.left if image_background_bbox.top > pasted_image_bbox.top: pasted_image_y_offset_from = -pasted_image_bbox.top pasted_image_x_offset_to = min( pasted_image_x_offset_from + intersection_bbox.width - 1, pasted_image_info.width) pasted_image_y_offset_to = min( pasted_image_y_offset_from + intersection_bbox.height - 1, pasted_image_info.height) image_background_y_offset_from, image_background_x_offset_from, image_background_y_offset_to, image_background_x_offset_to = 0, 0, 0, 0 if x_offset >= 0: image_background_x_offset_from = x_offset if y_offset >= 0: image_background_y_offset_from = y_offset image_background_x_offset_to = image_background_x_offset_from + pasted_image_x_offset_to - pasted_image_x_offset_from image_background_y_offset_to = image_background_y_offset_from + pasted_image_y_offset_to - pasted_image_y_offset_from for channel_index in range(3): image_background[image_background_y_offset_from : image_background_y_offset_to, image_background_x_offset_from : image_background_x_offset_to, channel_index] = \ (pasted_image[pasted_image_y_offset_from : pasted_image_y_offset_to, pasted_image_x_offset_from : pasted_image_x_offset_to, channel_index] * (pasted_image_mask[pasted_image_y_offset_from : pasted_image_y_offset_to, pasted_image_x_offset_from : pasted_image_x_offset_to] / 255)) + \ (image_background[image_background_y_offset_from : image_background_y_offset_to, image_background_x_offset_from : image_background_x_offset_to, channel_index] * (1 - (pasted_image_mask[pasted_image_y_offset_from : pasted_image_y_offset_to, pasted_image_x_offset_from : pasted_image_x_offset_to] / 255))) return image_background
def test_intersect_diagonal_interesction(self): bounding_box = BoundingBox(5, 5, 15, 15) bounding_box_to_intersect = BoundingBox(-10, -15, 6, 6) bounding_box_expected_intersection = BoundingBox(5, 5, 6, 6) self.assertEqual(bounding_box.intersect(bounding_box_to_intersect), bounding_box_expected_intersection)
def test_intersect_right_interesction(self): bounding_box = BoundingBox(5, 5, 15, 15) bounding_box_to_intersect = BoundingBox(-10, 15, 20, 16) bounding_box_expected_intersection = BoundingBox(5, 15, 15, 15) self.assertEqual(bounding_box.intersect(bounding_box_to_intersect), bounding_box_expected_intersection)