From b918c06032b2a84bd0cf0ed0f6d745c751fa8b56 Mon Sep 17 00:00:00 2001 From: Titusz Date: Thu, 14 May 2026 22:23:52 +0200 Subject: [PATCH] fix: align preprocess_image preflight order with IEP-0004 Swap trim_border and remove_transparency in preprocess_image so the order matches IEP-0004 (exif_transpose -> remove_transparency -> trim_border -> resize) and iscc-sdk.image_normalize. Running remove_transparency first gives trim_border a deterministic, encoder-independent reference pixel and avoids alpha-edge halos during the subsequent resize. For opaque inputs the resulting tensor is bit-identical; only inputs combining transparency and a uniform border can produce different Semantic-Codes. Pre-1.0 the project allows this kind of change. Refs #1 --- iscc_sci/code_semantic_image.py | 6 +++--- tests/test_iscc_sci.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/iscc_sci/code_semantic_image.py b/iscc_sci/code_semantic_image.py index 6125110..c8d21e6 100644 --- a/iscc_sci/code_semantic_image.py +++ b/iscc_sci/code_semantic_image.py @@ -127,15 +127,15 @@ def preprocess_image(image): # Transpose the image according to its orientation tag (if available). image = ImageOps.exif_transpose(image) + # Convert to RGB and add a white background if the image contains transparency. + image = remove_transparency(image) + # Crop uniformly colored borders if applicable. image = trim_border(image) # Resize the image image = image.resize((512, 512), Resampling.BILINEAR) - # Convert to RGB and add a white background if the image contains transparency. - image = remove_transparency(image) - # Convert to a numpy array and ensure type consistency image = np.array(image, dtype=np.float32) diff --git a/tests/test_iscc_sci.py b/tests/test_iscc_sci.py index 8436db1..3d363b2 100644 --- a/tests/test_iscc_sci.py +++ b/tests/test_iscc_sci.py @@ -18,7 +18,7 @@ def test_code_image_semantic_default(): def test_code_image_semantic_256bit(): result = sci.code_image_semantic(images()[1], bits=256) - assert result["iscc"] == "ISCC:CEDQ2WT7K2Q7YTO47HLGYUURO2RCI24K5VUZOHFMMY42C6O6VLQ6FEA" + assert result["iscc"] == "ISCC:CEDQ2UTPK2Q7ZTK47HLOYUUTO3TCA36INUUZODFMMY46S6O6VLSWFEA" def test_code_image_semantic_embedding_precision(): @@ -27,8 +27,8 @@ def test_code_image_semantic_embedding_precision(): assert result["features"][0]["maintype"] == "semantic" assert result["features"][0]["subtype"] == "image" assert result["features"][0]["version"] == 0 - assert result["features"][0]["embedding"][0] == -0.0557 - assert result["features"][0]["embedding"][-1] == -0.1192 + assert result["features"][0]["embedding"][0] == -0.0647 + assert result["features"][0]["embedding"][-1] == -0.109 def test_gen_image_code_semantic(img_array):