Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add util.convert_image_to_format_of #2

Merged
merged 3 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion giatools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
VERSION_MAJOR = 0
VERSION_MINOR = 1
VERSION_PATCH = 1
VERSION_PATCH = 2

VERSION = '%d.%d%s' % (VERSION_MAJOR, VERSION_MINOR, '.%d' % VERSION_PATCH if VERSION_PATCH > 0 else '')
43 changes: 43 additions & 0 deletions giatools/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
Copyright 2017-2024 Leonid Kostrykin, Biomedical Computer Vision Group, Heidelberg University.

Distributed under the MIT license.
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
"""

import numpy as np
import skimage.util


def convert_image_to_format_of(image, format_image):
"""
Convert the first image to the format of the second image.
"""

# There is nothing to do with the image if the formats match.
if format_image.dtype == image.dtype:
return image

# Convert the image to uint8 if this is the format of the second image.
elif format_image.dtype == np.uint8:
return skimage.util.img_as_ubyte(image)

# Convert the image to uint16 if this is the format of the second image.
elif format_image.dtype == np.uint16:
return skimage.util.img_as_uint(image)

# Convert the image to int16 if this is the format of the second image.
elif format_image.dtype == np.int16:
return skimage.util.img_as_int(image)

# Convert the image to float32 if this is the format of the second image.
elif format_image.dtype == np.float32:
return skimage.util.img_as_float32(image)

# Convert the image to float64 if this is the format of the second image.
elif format_image.dtype == np.float64:
return skimage.util.img_as_float64(image)

# Other formats are not supported yet (e.g., float16).
else:
raise ValueError(f'Unsupported image data type: {format_image.dtype}')
1 change: 1 addition & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest

import giatools.io


Expand Down
38 changes: 38 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import itertools
import unittest

import numpy as np
import skimage.util

import giatools.util


class convert_image_to_format_of(unittest.TestCase):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
np.random.seed(0)
img_uint8 = (np.random.rand(16, 16) * 255).round().astype(np.uint8)
self.testdata = [
skimage.util.img_as_ubyte(img_uint8), # uint8
skimage.util.img_as_uint(img_uint8), # uint16
skimage.util.img_as_int(img_uint8), # int16
skimage.util.img_as_float32(img_uint8), # float32
skimage.util.img_as_float64(img_uint8), # float64
]

def test_self_conversion(self):
for img in self.testdata:
actual = giatools.util.convert_image_to_format_of(img, img)
self.assertIs(actual, img)

def test_cross_conversion(self):
for src_img, dst_img in itertools.product(self.testdata, self.testdata):
with self.subTest(f'{src_img.dtype} -> {dst_img.dtype}'):
actual = giatools.util.convert_image_to_format_of(src_img, dst_img)
self.assertEqual(actual.dtype, dst_img.dtype)
self.assertTrue(np.allclose(actual, dst_img, rtol=1e-2))


if __name__ == '__main__':
unittest.main()