def test_crop_flip_complex(self, resources): expected_result = resources / "imforge" / "crop" / "expected" / "cv2" / "crop_flip_complex.png" crop_box = [(15, 8), (-15, 50), (200, 200), (450, 161), (368, 78)] image = cv2.imread(str(resources / "some_text.jpg"), cv2.IMREAD_UNCHANGED) cropped_image = crop_cv2(image, crop_box, fillcolor=(255, 0, 255), cut_out=True, clip=True) # Use PIL image for saving and comparison of result cropped_image = Image.fromarray(cv2.cvtColor(cropped_image, cv2.COLOR_BGR2RGB), "RGB") if self.DEBUG: cropped_image.show() if not expected_result.exists(): cropped_image.save(expected_result, optimize=True) image_bytes = BytesIO() cropped_image.save(image_bytes, format="png", optimize=True) image_bytes.seek(0) assert image_bytes.read() == expected_result.read_bytes()
def test_crop_near_edge(self, resources): expected_result = resources / "imforge" / "crop" / "expected" / "cv2" / "crop_near_edge.png" crop_box = [(102, 750), (101, 529), (324, 528), (322, 750)] image = cv2.imread(str(resources / "qrcode_multi.png"), cv2.IMREAD_UNCHANGED) cropped_image = crop_cv2(image, crop_box, fillcolor=(255, 0, 255)) # Use PIL image for saving and comparison of result cropped_image = Image.fromarray(cv2.cvtColor(cropped_image, cv2.COLOR_BGR2RGB), "RGB") if self.DEBUG: cropped_image.show() if not expected_result.exists(): cropped_image.save(expected_result, optimize=True) image_bytes = BytesIO() cropped_image.save(image_bytes, format="png", optimize=True) image_bytes.seek(0) assert image_bytes.read() == expected_result.read_bytes()
def test_full_clip(self, resources): expected_result = resources / "imforge" / "crop" / "expected" / "cv2" / "full_clip.png" crop_box = [(-10, -10), (400, -10), (400, 200), (-10, 200)] image = cv2.imread(str(resources / "some_text.jpg"), cv2.IMREAD_UNCHANGED) cropped_image = crop_cv2(image, crop_box, clip=True) # Use PIL image for saving and comparison of result cropped_image = Image.fromarray(cv2.cvtColor(cropped_image, cv2.COLOR_BGR2RGB), "RGB") if self.DEBUG: cropped_image.show() if not expected_result.exists(): cropped_image.save(expected_result, optimize=True) image_bytes = BytesIO() cropped_image.save(image_bytes, format="png", optimize=True) image_bytes.seek(0) assert image_bytes.read() == expected_result.read_bytes()