Loading imgutils/data/image.py +4 −0 Original line number Diff line number Diff line Loading @@ -107,8 +107,12 @@ def load_image(image: ImageTyping, mode=None, force_background: Optional[str] = 'RGB' """ from .blob import is_valid_image_blob_url, load_image_from_blob_url from .url import is_http_url, download_image_from_url if isinstance(image, str) and is_valid_image_blob_url(image): image = load_image_from_blob_url(image) elif isinstance(image, str) and is_http_url(image): image = download_image_from_url(image, silent=True) elif isinstance(image, (str, PathLike, bytes, bytearray, BinaryIO)) or _is_readable(image): image = Image.open(image) elif isinstance(image, Image.Image): Loading test/data/test_image.py +16 −0 Original line number Diff line number Diff line Loading @@ -146,3 +146,19 @@ class TestHasAlphaChannel: def test_pa_image(self): pa_image = Image.new('PA', (10, 10)) assert has_alpha_channel(pa_image) @pytest.mark.parametrize(['url', 'local_image'], [ ( 'https://github.com/deepghs/imgutils/blob/main/test/testfile/nian_640.png', ('nian_640.png',) ), ( 'https://huggingface.co/deepghs/eattach_monochrome_experiments/blob/main/mlp_layer1_seed1/plot_confusion.png', ('plot_confusion.png',) ) ]) def test_load_image_from_url(self, url, local_image, image_diff): local_image_file = get_testfile(*local_image) actual_image = load_image(url, mode='RGB', force_background='white') expected_image = load_image(local_image_file, mode='RGB', force_background='white') assert image_diff(actual_image, expected_image, throw_exception=False) < 1e-2 Loading
imgutils/data/image.py +4 −0 Original line number Diff line number Diff line Loading @@ -107,8 +107,12 @@ def load_image(image: ImageTyping, mode=None, force_background: Optional[str] = 'RGB' """ from .blob import is_valid_image_blob_url, load_image_from_blob_url from .url import is_http_url, download_image_from_url if isinstance(image, str) and is_valid_image_blob_url(image): image = load_image_from_blob_url(image) elif isinstance(image, str) and is_http_url(image): image = download_image_from_url(image, silent=True) elif isinstance(image, (str, PathLike, bytes, bytearray, BinaryIO)) or _is_readable(image): image = Image.open(image) elif isinstance(image, Image.Image): Loading
test/data/test_image.py +16 −0 Original line number Diff line number Diff line Loading @@ -146,3 +146,19 @@ class TestHasAlphaChannel: def test_pa_image(self): pa_image = Image.new('PA', (10, 10)) assert has_alpha_channel(pa_image) @pytest.mark.parametrize(['url', 'local_image'], [ ( 'https://github.com/deepghs/imgutils/blob/main/test/testfile/nian_640.png', ('nian_640.png',) ), ( 'https://huggingface.co/deepghs/eattach_monochrome_experiments/blob/main/mlp_layer1_seed1/plot_confusion.png', ('plot_confusion.png',) ) ]) def test_load_image_from_url(self, url, local_image, image_diff): local_image_file = get_testfile(*local_image) actual_image = load_image(url, mode='RGB', force_background='white') expected_image = load_image(local_image_file, mode='RGB', force_background='white') assert image_diff(actual_image, expected_image, throw_exception=False) < 1e-2