Disable footer via stylesheet instead of skipping it

This commit is contained in:
augustin64 2024-07-05 23:41:42 +02:00
parent e6a7c17435
commit a2f3fb0848

View File

@ -12,23 +12,23 @@ from modules import logger
RATIO = 16/9 # 16:9 RATIO = 16/9 # 16:9
def get_elements(root, attribute):
return [i for i in root if i.tag == attribute]
def get_element(root, attribute):
elems = get_elements(root, attribute)
if len(elems) > 1:
print(f"More than one '{attribute}' found", file=sys.stderr)
if len(elems) == 0:
raise IndexError(f"No '{attribute}' found !")
return elems[0]
def create_pagebreaks(data): def create_pagebreaks(data):
""" """
Replaces all manual linebreaks by pagebreaks in a .mscx file (xml) Replaces all manual linebreaks by pagebreaks in a .mscx file (xml)
""" """
def get_elements(root, attribute):
return [i for i in root if i.tag == attribute]
def get_element(root, attribute):
elems = get_elements(root, attribute)
if len(elems) > 1:
print(f"More than one '{attribute}' found", file=sys.stderr)
if len(elems) == 0:
raise IndexError(f"No '{attribute}' found !")
return elems[0]
root = ElementTree.fromstring(data) root = ElementTree.fromstring(data)
score = get_element(root, "Score") score = get_element(root, "Score")
@ -51,7 +51,19 @@ def create_pagebreaks(data):
return ElementTree.tostring(root), changes return ElementTree.tostring(root), changes
def create_pagebreaks_mscz(source, dest): def remove_footer(data):
"""
Disable page footer in .mss (xml)
"""
root = ElementTree.fromstring(data)
style = get_element(root, "Style")
show_footer = get_element(style, "showFooter")
show_footer.text = 0
return ElementTree.tostring(root)
def prepare_mscz(source, dest):
shutil.copy(source, dest) shutil.copy(source, dest)
with zipfile.ZipFile(source) as inzip, zipfile.ZipFile(dest, "w") as outzip: with zipfile.ZipFile(source) as inzip, zipfile.ZipFile(dest, "w") as outzip:
for inzipinfo in inzip.infolist(): for inzipinfo in inzip.infolist():
@ -59,13 +71,16 @@ def create_pagebreaks_mscz(source, dest):
if inzipinfo.filename.endswith(".mscx"): if inzipinfo.filename.endswith(".mscx"):
new_data, changes = create_pagebreaks(infile.read()) new_data, changes = create_pagebreaks(infile.read())
outzip.writestr(inzipinfo.filename, new_data) outzip.writestr(inzipinfo.filename, new_data)
elif inzipinfo.filename.endswith(".mss"):
new_data = remove_footer(infile.read())
outzip.writestr(inzipinfo.filename, new_data)
else: else:
outzip.writestr(inzipinfo.filename, infile.read()) outzip.writestr(inzipinfo.filename, infile.read())
def generate_images(mscz_file, base_dest): def generate_images(mscz_file, base_dest):
with tempfile.TemporaryDirectory() as tmpdirname: with tempfile.TemporaryDirectory() as tmpdirname:
tmp_mscz = os.path.join(tmpdirname, "score.mscz") tmp_mscz = os.path.join(tmpdirname, "score.mscz")
create_pagebreaks_mscz(mscz_file, tmp_mscz) prepare_mscz(mscz_file, tmp_mscz)
logger.log(".mscz file patched") logger.log(".mscz file patched")
subprocess.call(["mscore", tmp_mscz, "-o", base_dest+".png"]) subprocess.call(["mscore", tmp_mscz, "-o", base_dest+".png"])
@ -92,7 +107,7 @@ def crop_image(file):
break break
min_non_white += 1 min_non_white += 1
for y in range(img.height-31, -1, -1): # Skip page number for y in range(img.height-1, -1, -1):
non_white = False non_white = False
for x in range(img.width): for x in range(img.width):
if img.getpixel((x, y)) != (255, 255, 255): if img.getpixel((x, y)) != (255, 255, 255):