Skip to content

Commit 355229f

Browse files
committed
dev&test(images):misnamed primitive
1 parent 45a22d5 commit 355229f

File tree

3 files changed

+66
-5
lines changed

3 files changed

+66
-5
lines changed

src/ZooProcess_lib/Extractor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from .bitmaps import one_mm_img
1010
from .img_tools import (
1111
cropnp,
12-
save_lossless_small_image,
12+
save_jpg_or_png_image,
1313
)
1414

1515

@@ -53,7 +53,7 @@ def extract_all_with_border_to_dir(
5353
else:
5454
# Save 1 byte/pixel, grey
5555
img_to_save = resized_img
56-
save_lossless_small_image(
56+
save_jpg_or_png_image(
5757
img_to_save,
5858
resolution,
5959
img_path,

src/ZooProcess_lib/img_tools.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77

88
import cv2
99
import numpy as np
10-
from PIL import Image
10+
from PIL import Image, ExifTags
1111
from PIL.ExifTags import Base, TAGS
1212
from PIL.ImageFile import ImageFile
13+
from PIL.PngImagePlugin import PngInfo
1314

1415
from .LegacyMeta import LutFile
1516
from .tools import timeit
@@ -1198,13 +1199,24 @@ def find_res(filename):
11981199
print("IMAGE RESOLUTION IS : ", width, "X", height)
11991200

12001201

1201-
def save_lossless_small_image(image: np.ndarray, resolution: int, path: Path):
1202+
def save_jpg_or_png_image(
1203+
image: np.ndarray, resolution: int, path: Path, description: Optional[str] = None
1204+
) -> None:
12021205
ext = path.suffix
12031206
assert ext in [".jpg", ".jpeg", ".png"]
12041207
pil_image = Image.fromarray(image)
12051208
options = {"optimize": True, "dpi": (resolution, resolution)}
12061209
if ext in (".jpg", ".jpeg"):
12071210
options["quality"] = 100 # Note: this does _not_ make the JPEG lossless
1211+
if description is not None:
1212+
if ext == ".png":
1213+
info = PngInfo()
1214+
info.add_text("Description", description)
1215+
options["pnginfo"] = info # type:ignore
1216+
else:
1217+
exif = Image.Exif()
1218+
exif[ExifTags.Base.ImageDescription] = description
1219+
options["exif"] = exif # type:ignore
12081220
pil_image.save(path, **options)
12091221

12101222

tests/test_img_tools.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import tempfile
2+
from pathlib import Path
3+
14
import numpy as np
25
import pytest
6+
from PIL import Image
37

4-
from ZooProcess_lib.img_tools import crop
8+
from ZooProcess_lib.img_tools import crop, image_info, save_jpg_or_png_image
59

610

711
def test_crop_basic():
@@ -54,3 +58,48 @@ def test_crop_input_validation():
5458
# Test with negative coordinates
5559
with pytest.raises(IndexError):
5660
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

Comments
 (0)