Commit 9d3b120a authored by inkhey's avatar inkhey

add support for non-transparency-background image

parent b9629bc5
import glob
import logging
import random
import typing
from PIL import Image
from pioupiou.exceptions import ImageGivenShouldHaveAlphaLayer
from pioupiou.exceptions import UnsupportedImageMode
LAYER_FILE_IMAGE_PATTERN = "{folder_path}/{layer_name}_[0-9]*.png"
PILLOW_ALPHA_MODES = ["RGBA", "LA", "RGBa"]
LOGGER_NAME = "pioupiou"
class Layer(object):
......@@ -21,12 +26,30 @@ class Layer(object):
self.variations_images_paths = variation_images_paths
self.name = name
def get_random_image(self) -> Image:
def get_random_image(self, allow_no_alpha_layer: bool = False) -> Image:
"""Get random image from layer based on current seed"""
assert self.variations_images_paths
image = Image.open(random.choice(self.variations_images_paths))
# INFO - GM - 03/07/2019 - We do not support image without alpha layer
assert image.mode in PILLOW_ALPHA_MODES
chosen_path = random.choice(self.variations_images_paths)
logging.getLogger(LOGGER_NAME).debug(
'open "{}" for layer "{}" of level "{}"'.format(chosen_path, self.name, self.level)
)
image = Image.open(chosen_path)
# INFO - GM - 03/07/2019 - We do not support image without alpha layer unless
if image.mode not in PILLOW_ALPHA_MODES:
if not allow_no_alpha_layer:
raise ImageGivenShouldHaveAlphaLayer(
"This image doesn't contain alpha layer but this layer is required"
"to process correctly this layer"
)
else:
if image.mode == "RGB":
image = image.convert("RGBA")
elif image.mode == "LA":
image = image.convert("L")
else:
raise UnsupportedImageMode(
"the mode {} of this image is unsupported".format(image.mode)
)
return image
......@@ -41,7 +64,7 @@ class AvatarTheme(object):
"""
random.seed(token)
sorted_layers = sorted(self.layers, key=lambda x: x.level)
current_image = sorted_layers[0].get_random_image()
current_image = sorted_layers[0].get_random_image(allow_no_alpha_layer=True)
for layer in sorted_layers[1:]:
current_image = Image.alpha_composite(current_image, layer.get_random_image())
return current_image
......
class PiouPiouException(Exception):
pass
class ImageGivenShouldHaveAlphaLayer(PiouPiouException):
pass
class UnsupportedImageMode(PiouPiouException):
pass
from PIL import Image
from pioupiou import AvatarTheme
from pioupiou import Layer
class TestAvatarTheme(object):
def test_avatar_theme__ok__nominal_case(self):
layer1 = Layer(
level=0, name="star", variation_images_paths=["tests/test_img/black_background.png"]
)
layer2 = Layer(
level=1, name="star", variation_images_paths=["tests/test_img/white_alpha_star.png"]
)
avatar_theme = AvatarTheme(layers=[layer1, layer2])
image = avatar_theme.generate_avatar("token")
assert image.mode == "RGBA"
avatar_theme.save_on_disk(image, "/tmp/test_avatar.png")
with Image.open("/tmp/test_avatar.png") as avatar:
assert avatar.height == 20
assert avatar.width == 20
import pytest
from pioupiou import Layer
from pioupiou.exceptions import ImageGivenShouldHaveAlphaLayer
class TestLayer(object):
def test_unit__layer__ok__nominal_case(self):
layer = Layer(
level=0, name="star", variation_images_paths=["tests/test_img/white_alpha_star.png"]
)
image = layer.get_random_image(allow_no_alpha_layer=False)
assert image.format == "PNG"
assert image.mode == "RGBA"
def test_unit__layer__err__image_should_have_alpha_layer(self):
layer = Layer(
level=0, name="star", variation_images_paths=["tests/test_img/black_background.png"]
)
with pytest.raises(ImageGivenShouldHaveAlphaLayer):
layer.get_random_image(allow_no_alpha_layer=False)
def test_unit__layer__err__ok__allow_alpha_laver(self):
layer = Layer(
level=0, name="star", variation_images_paths=["tests/test_img/black_background.png"]
)
image = layer.get_random_image(allow_no_alpha_layer=True)
assert image.mode == "RGBA"
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment