esmaill1
feat: implement image processing core, FastAPI backend, and full-stack integration tests
f19ba0f | import sys | |
| import os | |
| import unittest | |
| import tempfile | |
| from pathlib import Path | |
| from PIL import Image | |
| # Add root and core directories to python path | |
| ROOT_DIR = Path(__file__).resolve().parent.parent | |
| sys.path.insert(0, str(ROOT_DIR)) | |
| sys.path.insert(0, str(ROOT_DIR / "core")) | |
| import crop | |
| class TestCrop(unittest.TestCase): | |
| def setUp(self): | |
| # Create a temporary directory for test files | |
| self.test_dir = tempfile.TemporaryDirectory() | |
| self.img_path = os.path.join(self.test_dir.name, "test_input.jpg") | |
| self.out_path = os.path.join(self.test_dir.name, "test_output.jpg") | |
| # Create a synthetic 1000x1400 image (already at 5:7 aspect ratio) | |
| img = Image.new("RGB", (1000, 1400), (100, 150, 200)) | |
| img.save(self.img_path) | |
| def tearDown(self): | |
| # Clean up temporary directory | |
| self.test_dir.cleanup() | |
| def test_get_auto_crop_rect_fallback(self): | |
| # Since the synthetic image is just solid color, no face will be detected. | |
| # It should fall back to center crop. | |
| rect = crop.get_auto_crop_rect(self.img_path) | |
| self.assertIsNotNone(rect) | |
| x1, y1, x2, y2 = rect | |
| self.assertTrue(0 <= x1 < x2) | |
| self.assertTrue(0 <= y1 < y2) | |
| # Check aspect ratio is 5:7 | |
| width = x2 - x1 | |
| height = y2 - y1 | |
| self.assertAlmostEqual(width / height, 5.0 / 7.0, places=2) | |
| def test_apply_custom_crop(self): | |
| rect = (100, 100, 600, 800) # aspect ratio 5:7 (500x700) | |
| success = crop.apply_custom_crop(self.img_path, self.out_path, rect) | |
| self.assertTrue(success) | |
| self.assertTrue(os.path.exists(self.out_path)) | |
| # Load output and check size is exactly 1181x1654 | |
| with Image.open(self.out_path) as out_img: | |
| self.assertEqual(out_img.size, (1181, 1654)) | |
| self.assertEqual(out_img.info.get("dpi"), (300, 300)) | |
| def test_crop_to_4x6_opencv(self): | |
| success = crop.crop_to_4x6_opencv(self.img_path, self.out_path) | |
| self.assertTrue(success) | |
| self.assertTrue(os.path.exists(self.out_path)) | |
| with Image.open(self.out_path) as out_img: | |
| self.assertEqual(out_img.size, (1181, 1654)) | |
| if __name__ == "__main__": | |
| unittest.main() | |