diff --git a/src/generate_images.py b/src/generate_images.py index e02b9b2..7d4b638 100644 --- a/src/generate_images.py +++ b/src/generate_images.py @@ -6,6 +6,11 @@ import shutil import sys import os +from PIL import Image, ImageOps +from modules import logger + +RATIO = 16/9 # 16:9 + def create_pagebreaks(data): """ @@ -18,7 +23,7 @@ def create_pagebreaks(data): elems = get_elements(root, attribute) if len(elems) > 1: - print(f"More than one '{attribute}' found") + print(f"More than one '{attribute}' found", file=sys.stderr) if len(elems) == 0: raise IndexError(f"No '{attribute}' found !") @@ -54,19 +59,75 @@ def create_pagebreaks_mscz(source, dest): if inzipinfo.filename.endswith(".mscx"): new_data, changes = create_pagebreaks(infile.read()) outzip.writestr(inzipinfo.filename, new_data) - print(f"Modified {inzipinfo.filename} : {changes} elements") else: outzip.writestr(inzipinfo.filename, infile.read()) - print(f"Copied {inzipinfo.filename}") def generate_images(mscz_file, base_dest): with tempfile.TemporaryDirectory() as tmpdirname: tmp_mscz = os.path.join(tmpdirname, "score.mscz") create_pagebreaks_mscz(mscz_file, tmp_mscz) + logger.log(".mscz file patched") subprocess.call(["mscore", tmp_mscz, "-o", base_dest+".png"]) subprocess.call(["mscore", mscz_file, "-o", base_dest+"-short.png"]) - print("Images created") + logger.log("Images generated") + +def crop_image(file): + """ + Crop white bottom and top of an image + """ + def pixel_diff(px0, px1): + return abs(px0[0]-px1[0])+abs(px0[1]-px1[1])+abs(px0[2]-px1[2]) + + img = Image.open(file).convert("RGB") + + min_non_white, max_non_white = 0, img.height + for y in range(img.height): + non_white = False + for x in range(img.width): + if img.getpixel((x, y)) != (255, 255, 255): + non_white = True + break + if non_white: + break + min_non_white += 1 + + for y in range(img.height-31, -1, -1): # Skip page number + non_white = False + for x in range(img.width): + if img.getpixel((x, y)) != (255, 255, 255): + non_white = True + break + if non_white: + break + max_non_white -= 1 + + img.crop((0, min_non_white, img.width-1, max_non_white)).save(file) + return img.width, max_non_white - min_non_white + + +def adjust_images(images): + width, height = -1, -1 + for img_file in images: + logger.log(f"Cropping {img_file}") + w, h = crop_image(img_file) + width = width if w < width else w + height = height if h < height else h + + logger.log("Images cropped") + + width += 50 # Add some padding + height += 50 + if width / height < RATIO: + width = int(height*RATIO) + else: + height = int(width/RATIO) + + for img_file in images: + img = Image.open(img_file).convert("RGB") + ImageOps.pad(img, size=(width, height), color="white").save(img_file) + + logger.log("Padding added") if __name__ == "__main__": @@ -75,3 +136,8 @@ if __name__ == "__main__": sys.exit(1) generate_images(sys.argv[1], sys.argv[2]) + adjust_images([ + img for img in os.listdir(os.path.dirname(sys.argv[2])) if + img.startswith(os.path.basename(sys.argv[2])) and img.endswith(".png") and "short" not in img + ]) + diff --git a/src/modules/logger.py b/src/modules/logger.py new file mode 100644 index 0000000..116a8ca --- /dev/null +++ b/src/modules/logger.py @@ -0,0 +1,6 @@ +import time + +starting_time = time.time() + +def log(*args, **kwargs): + print(f"[{round(time.time()-starting_time, 3):>8}] ", *args, **kwargs)