From 2b9ab769b35257aefc5a0ea4f85e08a3ceea7657 Mon Sep 17 00:00:00 2001 From: algol Date: Thu, 30 Apr 2026 14:07:14 +0100 Subject: [PATCH 1/2] fix allowing string to watermarks --- httomolib/misc/images.py | 23 +++++++++++++++-------- pyproject.toml | 4 ++-- tests/test_misc/test_images.py | 20 ++++++++++++++++++++ 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/httomolib/misc/images.py b/httomolib/misc/images.py index 79809ad..5da20af 100644 --- a/httomolib/misc/images.py +++ b/httomolib/misc/images.py @@ -176,12 +176,16 @@ def save_to_images( Image.fromarray(d).save(filepath_name, quality=jpeg_quality) # after saving the image we check if the watermark needs to be added to that image - if watermark_vals is not None: - dec_points = __find_decimals(watermark_vals[idx]) - string_to_format = "." + str(dec_points) + "f" + if watermark_vals is not None: + if isinstance(watermark_vals[idx], str): + formatted_string = watermark_vals[idx] + else: + dec_points = __find_decimals(watermark_vals[idx]) + string_to_format = "." + str(dec_points) + "f" + formatted_string = format(watermark_vals[idx], string_to_format) _add_watermark( filepath_name, - format(watermark_vals[idx], string_to_format), + formatted_string, fill_val, stroke_val, ) @@ -206,15 +210,18 @@ def save_to_images( # after saving the image we check if the watermark needs to be added to that image if watermark_vals is not None: - dec_points = __find_decimals(watermark_vals[0]) - string_to_format = "." + str(dec_points) + "f" + if isinstance(watermark_vals[0], str): + formatted_string = watermark_vals[0] + else: + dec_points = __find_decimals(watermark_vals[0]) + string_to_format = "." + str(dec_points) + "f" + formatted_string = format(watermark_vals[0], string_to_format) _add_watermark( filepath_name, - format(watermark_vals[0], string_to_format), + formatted_string, fill_val, stroke_val, ) - if asynchronous: # Start the event loop to save the images - and wait until it's done assert queue is not None diff --git a/pyproject.toml b/pyproject.toml index b3be04b..bc0faa3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,9 +33,9 @@ authors = [ ] classifiers = [ "Development Status :: 4 - Beta", - "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.12", ] -requires-python = ">=3.10" +requires-python = ">=3.12" dynamic = ["version"] dependencies = [ "numpy", diff --git a/tests/test_misc/test_images.py b/tests/test_misc/test_images.py index 988b056..9b0a500 100644 --- a/tests/test_misc/test_images.py +++ b/tests/test_misc/test_images.py @@ -44,6 +44,26 @@ def test_save_to_images_watermark( assert f.name[-3:] == "tif" assert Image.open(f).size == (host_data.shape[2], host_data.shape[0]) +@pytest.mark.parametrize( + "dtype", [np.uint8, np.uint16, np.uint32, np.float32, np.float64] +) +def test_save_to_images_watermark_strings( + host_data: np.ndarray, tmp_path: pathlib.Path, dtype: np.dtype +): + + images = host_data[:, 50:53, :].astype(dtype) + watermark_vals = ('one', 'two', 'three') + save_to_images(images, tmp_path / "save_to_images", watermark_vals=watermark_vals) + + folder = tmp_path / "save_to_images" / f"images{dtype(0).nbytes * 8}bit_tif" + assert folder.exists() + files = list(folder.glob("*")) + + assert len(files) == 3 + for f in files: + assert f.name[-3:] == "tif" + assert Image.open(f).size == (host_data.shape[2], host_data.shape[0]) + @pytest.mark.parametrize("dtype", [np.uint8]) @pytest.mark.parametrize("file_format", ["jpeg", "png"]) From 91bea581fe85ae03a8d9dd11d38e641b5efd977f Mon Sep 17 00:00:00 2001 From: algol Date: Thu, 30 Apr 2026 14:29:19 +0100 Subject: [PATCH 2/2] updating CI job --- .github/workflows/httomolib_tests_run.yaml | 12 ++++++------ httomolib/misc/images.py | 4 ++-- tests/test_misc/test_images.py | 3 ++- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/.github/workflows/httomolib_tests_run.yaml b/.github/workflows/httomolib_tests_run.yaml index 7b3e8b9..ff10ede 100644 --- a/.github/workflows/httomolib_tests_run.yaml +++ b/.github/workflows/httomolib_tests_run.yaml @@ -18,17 +18,17 @@ jobs: steps: - name: Checkout repository code - uses: actions/checkout@v4 + uses: actions/checkout@v5 - # setup Python 3.10 - - name: Setup Python 3.10 - uses: actions/setup-python@v4 + # setup Python 3.12 + - name: Setup Python 3.12 + uses: actions/setup-python@v5 with: - python-version: '3.10' + python-version: '3.12' # install httomolib - name: Install httomolib - run: | + run: | pip install -e . pip list diff --git a/httomolib/misc/images.py b/httomolib/misc/images.py index 5da20af..e3a658d 100644 --- a/httomolib/misc/images.py +++ b/httomolib/misc/images.py @@ -176,7 +176,7 @@ def save_to_images( Image.fromarray(d).save(filepath_name, quality=jpeg_quality) # after saving the image we check if the watermark needs to be added to that image - if watermark_vals is not None: + if watermark_vals is not None: if isinstance(watermark_vals[idx], str): formatted_string = watermark_vals[idx] else: @@ -215,7 +215,7 @@ def save_to_images( else: dec_points = __find_decimals(watermark_vals[0]) string_to_format = "." + str(dec_points) + "f" - formatted_string = format(watermark_vals[0], string_to_format) + formatted_string = format(watermark_vals[0], string_to_format) _add_watermark( filepath_name, formatted_string, diff --git a/tests/test_misc/test_images.py b/tests/test_misc/test_images.py index 9b0a500..9c4e14c 100644 --- a/tests/test_misc/test_images.py +++ b/tests/test_misc/test_images.py @@ -44,6 +44,7 @@ def test_save_to_images_watermark( assert f.name[-3:] == "tif" assert Image.open(f).size == (host_data.shape[2], host_data.shape[0]) + @pytest.mark.parametrize( "dtype", [np.uint8, np.uint16, np.uint32, np.float32, np.float64] ) @@ -52,7 +53,7 @@ def test_save_to_images_watermark_strings( ): images = host_data[:, 50:53, :].astype(dtype) - watermark_vals = ('one', 'two', 'three') + watermark_vals = ("one", "two", "three") save_to_images(images, tmp_path / "save_to_images", watermark_vals=watermark_vals) folder = tmp_path / "save_to_images" / f"images{dtype(0).nbytes * 8}bit_tif"