Init project

This commit is contained in:
2023-04-26 17:09:19 +02:00
commit b2f76f3a0d
12 changed files with 2095 additions and 0 deletions

View File

@@ -0,0 +1,91 @@
import glob
from PIL import Image, ImageStat
# from _pytest.outcomes import skip
from iteration_utilities import duplicates
from pathlib import Path
import json
import os
import pytest
def validate_brightness_image(working_path, theme_config, high_light, image_list, brightness_way):
# get a dict<filename:string, mean:int> of all images to scan them
image_pattern = theme_config.get("imageFilename")
# if image_filenames is None:
image_filenames = {}.fromkeys(glob.glob(str(Path(working_path, image_pattern))))
# generate an image statistics for each images
for an_imagefile in image_filenames:
this_image = Image.open(an_imagefile).convert("L")
this_image_stats = ImageStat.Stat(this_image)
image_filenames[an_imagefile] = this_image_stats.mean[0]
# get the brightest observed image from the list
if high_light == "dayHighlight":
actual_est_image = max(image_filenames, key=lambda key: image_filenames[key])
else:
actual_est_image = min(image_filenames, key=lambda key: image_filenames[key])
# get the brightest referenced image OR in the daylist to compare to
if theme_config.get(high_light):
an_image = image_pattern.replace("*", str(theme_config.get(high_light)))
ref_est_file = str(Path(working_path, an_image))
assert actual_est_image == ref_est_file, f"{brightness_way} image is {actual_est_image}, but the {high_light} image is {ref_est_file}."
else:
# if not highlight is given in theme.json, we suppose that brightest images are in the daylist
ref_est_files = []
for an_id in theme_config.get(image_list):
an_image = image_pattern.replace('*', str(an_id))
ref_est_files.append(str(Path(working_path, an_image)))
assert actual_est_image in ref_est_files, f"{brightness_way} image {actual_est_image} was not found in the '{image_list}' attribute."
# print(f"✔ {brightness_way} image is {actual_est_image}, as expected.")
@pytest.fixture
def working_path():
# get the global variables containing gitlab-given project slug.
project_slug = os.environ["WPP_SLUG"]
root_folder = os.environ["WPP_ROOT"]
working_path = Path(root_folder, project_slug)
if not working_path.is_dir():
raise FileNotFoundError(f"No project found for the given {working_path}.")
return working_path
@pytest.fixture
def manifest(working_path):
with open(Path(working_path, "theme.json"), 'r') as fileobj:
return json.load(fileobj)
def test_brightest_image(working_path, manifest):
validate_brightness_image(working_path, manifest, "dayHighlight", "dayImageList", "Brightest")
def test_darkest_image(working_path, manifest):
validate_brightness_image(working_path, manifest, "nightHighlight", "nightImageList", "Darkest")
def test_image_size(working_path, manifest):
image_filename = manifest["imageFilename"].replace("*", str(manifest["dayImageList"][0]))
img = Image.open(Path(working_path, image_filename))
w, h = img.size
assert w >= 1920 and h >= 1080, f"✖ Image size is too small (must be at least 1920×1080, is {w}×{h})"
assert w >= h, "✖ Image orientation is portrait (must be landscape or square)"
# print(f"✔ Images are big enough ({w}×{h}) and landscape.")
def test_overlapping_images(manifest):
jointed_lists = manifest.get("dayImageList")
jointed_lists.extend(manifest.get("nightImageList"))
jointed_lists.extend(manifest.get("sunriseImageList", []))
jointed_lists.extend(manifest.get("sunsetImageList", []))
dup_refs = list(duplicates(jointed_lists))
str_dup_refs = [manifest["imageFilename"].replace("*", str(int)) for int in dup_refs]
separator = ", "
assert not dup_refs, f"✖ Some images are referenced twice or more times: {separator.join(str_dup_refs)}."
# print("✔ There is no overlapping references of images in the theme.json.")