Commit 650d984b authored by narugo1992's avatar narugo1992
Browse files

dev(narugo): refactor the fn loading

parent 2958f7a3
Loading
Loading
Loading
Loading
+9 −2
Original line number Diff line number Diff line
@@ -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
+51 −1
Original line number Diff line number Diff line
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 @@ class TestDataImage:
        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():
+1.15 MiB
Loading image diff...
+1.15 MiB
Loading image diff...
+1.15 MiB
Loading image diff...
Loading