# pyright: reportWildcardImportFromLibrary=false from behave import * # noqa: F403 from shutil import copyfile import os import time from PIL import Image, PngImagePlugin, ImageOps from exif import Image as ExifImage try: from steps.common import Case except ModuleNotFoundError: from common import Case def add_text_to_png(pngfile, texts: dict[str, str]): info = PngImagePlugin.PngInfo() for key, value in texts.items(): info.add_text(key, value) im = Image.open(pngfile) im.save(pngfile, pnginfo=info) @given(u'an SVG file as input') # noqa: F405 def step_svg_as_input(context): case: Case = context.case case.copy_and_set_infile( os.path.join(context.image_template_path, "some_svg.svg") ) @given(u'a JPEG image as input') # noqa: F405 def step_a_jpg_as_input(context): case: Case = context.case case.copy_and_set_infile(context.test_images["1200.jpg"]) @given(u'a JPEG image with edge size 100 as input') # noqa: F405 def step_a_jpg_100_as_input(context): case: Case = context.case case.copy_and_set_infile(context.test_images["100.jpg"]) @given(u'a JPEG image with edge size 500 as input') # noqa: F405 def step_a_jpg_500_as_input(context): case: Case = context.case case.copy_and_set_infile(context.test_images["500.jpg"]) @given(u'the {size} thumb image file is used as input') # noqa: F405 def step_a_thumb_is_used_as_input(context, size): case: Case = context.case input_path = None for path in case.thumb_paths: path_elements = path.split("/") if size == path_elements[-2]: assert input_path is None input_path = path assert input_path is not None case.hard_set_infile(input_path) @given(u'a 16-bit RGB PNG image as input') # noqa: F405 def step_a_16bit_rgb_png_as_input(context): case: Case = context.case case.copy_and_set_infile(context.test_images["1200_16bit_RGB.png"]) @given(u'thumbs exists for the input in standard DB') # noqa: F405 def step_thumbs_for_input_exists_in_std_db(context): case: Case = context.case for size, thumb_path in zip(case.sizes, case.thumb_paths): copyfile(context.test_images["thumb_{}".format(size)], thumb_path) add_text_to_png(thumb_path, {"Thumb::MTime": str(case.infile_mtime)}) case.prepared_thumbs_mtimes[thumb_path] = os.stat(thumb_path).st_mtime_ns time.sleep(0.01) @given(u'a thumb for size {size} exists for the input in standard DB') # noqa: F405 def step_thumb_of_size_for_input_exists_in_std_db(context, size): case: Case = context.case for s, thumb_path in zip(case.sizes, case.thumb_paths): if size != s: continue copyfile(context.test_images["thumb_{}".format(size)], thumb_path) add_text_to_png(thumb_path, {"Thumb::MTime": str(case.infile_mtime)}) case.prepared_thumbs_mtimes[thumb_path] = os.stat(thumb_path).st_mtime_ns time.sleep(0.01) return assert False, "Thumb of size {} could not be found by test framework.".format(size) @given(u'some thumbs exist') # noqa: F405 def step_some_thumbs_exists_in_std_db(context): step_a_jpg_as_input(context) step_thumbs_for_input_exists_in_std_db(context) @given(u'no thumbs exist') # noqa: F405 def step_no_thumbs_exist(context): case: Case = context.case assert len(case.prepared_thumbs_mtimes) == 0 os.makedirs(case.cache_dir) @given(u'the default cache directory does not exist') # noqa: F405 def step_the_default_cache_dir_does_not_exist(context): case: Case = context.case assert not os.path.exists(case.cache_dir) @given(u'the input image has Exif orientation {orientation_value}') # noqa: F405 def step_input_has_exif_orientation(context, orientation_value): orientation_value = int(orientation_value) case: Case = context.case jpg_path = case.infile assert isinstance(jpg_path, str) with open(jpg_path, 'rb') as image_file: exif_image = ExifImage(image_file) exif_image.orientation = orientation_value with open(jpg_path, 'wb') as new_image_file: new_image_file.write(exif_image.get_file()) @given(u'the input is {mirrored} mirrored and rotated by {angle} degrees counter-clockwise') # noqa: F405,E501 def step_input_is_mirrored_and_rotated(context, mirrored, angle): assert mirrored in ["not", "horizontally"] is_mirrored = mirrored == "horizontally" angle = int(angle) case: Case = context.case infile_path = case.infile assert isinstance(infile_path, str) actual_pil_image = Image.open(infile_path) if is_mirrored: print("Mirroring input image.") actual_pil_image = ImageOps.mirror(actual_pil_image) if angle != 0: print("Rotating input image by {}.".format(angle)) actual_pil_image = actual_pil_image.rotate(angle) actual_pil_image.save(infile_path)