melpomene/zooms_generator.py

49 lines
1.4 KiB
Python

# Melpomene comic reader
# CC-BY-NC-SA https://git.aribaud.net/caribaud/melpomene/
import sys
import re
import xml.etree.ElementTree as ET
from pathlib import Path
def extract_zooms(src_folder, dest_file):
folder = Path(src_folder)
zooms = {}
for svg_path in folder.glob("*.svg"):
print(svg_path.name)
match = re.search("P(\d+)", svg_path.name)
if match:
page_idx = int(match.group(1))
zooms[page_idx] = []
tree = ET.parse(svg_path)
root = tree.getroot()
for area in root.findall('.//{*}rect'):
zooms[page_idx].append([
float(area.get("width")),
float(area.get("height")),
float(area.get("x")),
float(area.get("y")),
])
with open(dest_file, "w") as data_file:
data_file.write("zooms = [\n")
for page_idx in sorted(zooms.keys()):
for zoom in zooms[page_idx]:
if zoom[2] < 0 or zoom[3] < 0 :
print(f"WARNING: negative pos x / pos y in page {page_idx} for zoom {zoom} (is the rectangle flipped?)")
data_file.write(f" {[page_idx] + zoom},\n")
data_file.write("]\n")
if __name__ == "__main__":
extract_zooms(sys.argv[1], sys.argv[2])