|
| 1 | +import tempfile |
| 2 | +from pathlib import Path |
| 3 | + |
1 | 4 | import numpy as np
|
2 | 5 | import pytest
|
| 6 | +from PIL import Image |
3 | 7 |
|
4 |
| -from ZooProcess_lib.img_tools import crop |
| 8 | +from ZooProcess_lib.img_tools import crop, image_info, save_jpg_or_png_image |
5 | 9 |
|
6 | 10 |
|
7 | 11 | def test_crop_basic():
|
@@ -54,3 +58,48 @@ def test_crop_input_validation():
|
54 | 58 | # Test with negative coordinates
|
55 | 59 | with pytest.raises(IndexError):
|
56 | 60 | crop(test_image, top=-1, left=0, bottom=3, right=3)
|
| 61 | + |
| 62 | + |
| 63 | +def test_save_jpg_or_png_image_roundtrip(): |
| 64 | + """Test roundtrip for save_jpg_or_png_image, checking description and resolution.""" |
| 65 | + # Create a simple test image |
| 66 | + test_image = np.zeros((100, 100, 3), dtype=np.uint8) |
| 67 | + test_image[25:75, 25:75] = [255, 0, 0] # Red square in the middle |
| 68 | + |
| 69 | + # Test parameters |
| 70 | + test_resolution = 300 |
| 71 | + test_description = "Test image description" |
| 72 | + |
| 73 | + # Create a temporary directory for test files |
| 74 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 75 | + # Test with JPG format |
| 76 | + jpg_path = Path(temp_dir) / "test_image.jpg" |
| 77 | + save_jpg_or_png_image(test_image, test_resolution, jpg_path, test_description) |
| 78 | + |
| 79 | + # Load the JPG image and check metadata |
| 80 | + jpg_pil_image = Image.open(jpg_path) |
| 81 | + jpg_info = image_info(jpg_pil_image) |
| 82 | + |
| 83 | + # Verify resolution (allow for small floating-point differences) |
| 84 | + jpg_dpi_x, jpg_dpi_y = jpg_info["dpi"] |
| 85 | + assert abs(jpg_dpi_x - test_resolution) < 0.01 |
| 86 | + assert abs(jpg_dpi_y - test_resolution) < 0.01 |
| 87 | + |
| 88 | + # Verify description (in EXIF data for JPG) |
| 89 | + assert jpg_info.get("ImageDescription") == test_description |
| 90 | + |
| 91 | + # Test with PNG format |
| 92 | + png_path = Path(temp_dir) / "test_image.png" |
| 93 | + save_jpg_or_png_image(test_image, test_resolution, png_path, test_description) |
| 94 | + |
| 95 | + # Load the PNG image and check metadata |
| 96 | + png_pil_image = Image.open(png_path) |
| 97 | + png_info = image_info(png_pil_image) |
| 98 | + |
| 99 | + # Verify resolution (allow for small floating-point differences) |
| 100 | + png_dpi_x, png_dpi_y = png_info["dpi"] |
| 101 | + assert abs(png_dpi_x - test_resolution) < 0.01 |
| 102 | + assert abs(png_dpi_y - test_resolution) < 0.01 |
| 103 | + |
| 104 | + # Verify description (in text metadata for PNG) |
| 105 | + assert png_info.get("Description") == test_description |
0 commit comments