Skip to content

Commit

Permalink
dev(narugo): refactor the fn loading
Browse files Browse the repository at this point in the history
  • Loading branch information
narugo1992 committed Oct 30, 2024
1 parent 2958f7a commit 650d984
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 3 deletions.
11 changes: 9 additions & 2 deletions imgutils/data/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
52 changes: 51 additions & 1 deletion test/data/test_image.py
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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():
Expand Down
Binary file added test/testfile/mostima_post_bg_black.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/testfile/mostima_post_bg_blue.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/testfile/mostima_post_bg_green.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/testfile/mostima_post_bg_red.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/testfile/mostima_post_bg_white.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/testfile/nian_bg_black.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/testfile/nian_bg_blue.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/testfile/nian_bg_green.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/testfile/nian_bg_red.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/testfile/nian_bg_white.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 650d984

Please sign in to comment.