diff --git a/imgutils/data/image.py b/imgutils/data/image.py index 211cfa897e1..d9e5354f91a 100644 --- a/imgutils/data/image.py +++ b/imgutils/data/image.py @@ -180,5 +180,12 @@ def add_background_for_rgba(image: ImageTyping, background: str = 'white'): >>> rgb_image.mode 'RGB' """ - from .layer import istack - return istack(background, image).convert('RGB') + image = load_image(image, force_background=None, mode=None) + if has_alpha_channel(image): + ret_image = Image.new('RGBA', image.size, background) + ret_image.paste(image, (0, 0), mask=image) + else: + ret_image = image + if ret_image.mode != 'RGB': + ret_image = ret_image.convert('RGB') + return ret_image diff --git a/test/data/test_image.py b/test/data/test_image.py index 55616edc8fb..d4e365317ad 100644 --- a/test/data/test_image.py +++ b/test/data/test_image.py @@ -1,7 +1,7 @@ import pytest from PIL import Image -from imgutils.data import load_image, has_alpha_channel +from imgutils.data import load_image, has_alpha_channel, add_background_for_rgba from test.testings import get_testfile _FILENAME = get_testfile('6125785.png') @@ -24,6 +24,56 @@ def test_load_image(self, image_, result, image_diff): else: assert image_diff(load_image(image_), result, throw_exception=False) < 1e-2 + @pytest.mark.parametrize(['color'], [ + ('white',), + ('green',), + ('red',), + ('blue',), + ('black',), + ]) + def test_load_image_bg_rgba(self, image_diff, color): + image = load_image(get_testfile('nian.png'), force_background=color, mode='RGB') + expected = Image.open(get_testfile(f'nian_bg_{color}.png')) + assert image_diff(image, expected, throw_exception=False) < 1e-2 + + @pytest.mark.parametrize(['color'], [ + ('white',), + ('green',), + ('red',), + ('blue',), + ('black',), + ]) + def test_add_background_for_rgba_rgba(self, image_diff, color): + image = add_background_for_rgba(get_testfile('nian.png'), background=color) + assert image.mode == 'RGB' + expected = Image.open(get_testfile(f'nian_bg_{color}.png')) + assert image_diff(image, expected, throw_exception=False) < 1e-2 + + @pytest.mark.parametrize(['color'], [ + ('white',), + ('green',), + ('red',), + ('blue',), + ('black',), + ]) + def test_load_image_bg_rgb(self, image_diff, color): + image = load_image(get_testfile('mostima_post.jpg'), force_background=color, mode='RGB') + expected = Image.open(get_testfile(f'mostima_post_bg_{color}.png')) + assert image_diff(image, expected, throw_exception=False) < 1e-2 + + @pytest.mark.parametrize(['color'], [ + ('white',), + ('green',), + ('red',), + ('blue',), + ('black',), + ]) + def test_add_backround_for_rgba_rgb(self, image_diff, color): + image = add_background_for_rgba(get_testfile('mostima_post.jpg'), background=color) + assert image.mode == 'RGB' + expected = Image.open(get_testfile(f'mostima_post_bg_{color}.png')) + assert image_diff(image, expected, throw_exception=False) < 1e-2 + @pytest.fixture def rgba_image(): diff --git a/test/testfile/mostima_post_bg_black.png b/test/testfile/mostima_post_bg_black.png new file mode 100644 index 00000000000..6744f5342dc Binary files /dev/null and b/test/testfile/mostima_post_bg_black.png differ diff --git a/test/testfile/mostima_post_bg_blue.png b/test/testfile/mostima_post_bg_blue.png new file mode 100644 index 00000000000..6744f5342dc Binary files /dev/null and b/test/testfile/mostima_post_bg_blue.png differ diff --git a/test/testfile/mostima_post_bg_green.png b/test/testfile/mostima_post_bg_green.png new file mode 100644 index 00000000000..6744f5342dc Binary files /dev/null and b/test/testfile/mostima_post_bg_green.png differ diff --git a/test/testfile/mostima_post_bg_red.png b/test/testfile/mostima_post_bg_red.png new file mode 100644 index 00000000000..6744f5342dc Binary files /dev/null and b/test/testfile/mostima_post_bg_red.png differ diff --git a/test/testfile/mostima_post_bg_white.png b/test/testfile/mostima_post_bg_white.png new file mode 100644 index 00000000000..6744f5342dc Binary files /dev/null and b/test/testfile/mostima_post_bg_white.png differ diff --git a/test/testfile/nian_bg_black.png b/test/testfile/nian_bg_black.png new file mode 100644 index 00000000000..f028f1ec099 Binary files /dev/null and b/test/testfile/nian_bg_black.png differ diff --git a/test/testfile/nian_bg_blue.png b/test/testfile/nian_bg_blue.png new file mode 100644 index 00000000000..d0d73e1348d Binary files /dev/null and b/test/testfile/nian_bg_blue.png differ diff --git a/test/testfile/nian_bg_green.png b/test/testfile/nian_bg_green.png new file mode 100644 index 00000000000..9091318287b Binary files /dev/null and b/test/testfile/nian_bg_green.png differ diff --git a/test/testfile/nian_bg_red.png b/test/testfile/nian_bg_red.png new file mode 100644 index 00000000000..89bddbb2bb4 Binary files /dev/null and b/test/testfile/nian_bg_red.png differ diff --git a/test/testfile/nian_bg_white.png b/test/testfile/nian_bg_white.png new file mode 100644 index 00000000000..291e0a255b8 Binary files /dev/null and b/test/testfile/nian_bg_white.png differ