Commit 8f953532 authored by inkhey's avatar inkhey

use specific random generator object to allow more easily changing it and...

use specific random generator  object to allow more easily changing it and avoid unwanted side-effects
parent 6a3e4cc4
import glob
import logging
import random
from random import Random
import typing
from PIL import Image
......@@ -26,10 +27,12 @@ class Layer(object):
self.variations_images_paths = variation_images_paths
self.name = name
def get_random_image(self, allow_no_alpha_layer: bool = False) -> Image:
def get_random_image(
self, random_generator: Random = random, allow_no_alpha_layer: bool = False
) -> Image:
"""Get random image from layer based on current seed"""
assert self.variations_images_paths
chosen_path = random.choice(self.variations_images_paths)
chosen_path = random_generator.choice(self.variations_images_paths)
logging.getLogger(LOGGER_NAME).debug(
'open "{}" for layer "{}" of level "{}"'.format(chosen_path, self.name, self.level)
)
......@@ -56,17 +59,22 @@ class Layer(object):
class AvatarTheme(object):
def __init__(self, layers: typing.List[Layer]):
self.layers = layers
self.rng = Random()
def generate_avatar(self, token: str) -> Image:
"""
Generate avatar Image by obtaining layer and applying them
:param token: token used as seed to decide layer variation to use.
"""
random.seed(token)
self.rng.seed(token)
sorted_layers = sorted(self.layers, key=lambda x: x.level)
current_image = sorted_layers[0].get_random_image(allow_no_alpha_layer=True)
current_image = sorted_layers[0].get_random_image(
allow_no_alpha_layer=True, random_generator=self.rng
)
for layer in sorted_layers[1:]:
current_image = Image.alpha_composite(current_image, layer.get_random_image())
current_image = Image.alpha_composite(
current_image, layer.get_random_image(random_generator=self.rng)
)
return current_image
def save_on_disk(self, avatar: Image, path: str) -> str:
......
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