From 102fb78c0296fa447e7c9c95b055c4c6a1b49fae Mon Sep 17 00:00:00 2001 From: Ruben van de Ven Date: Fri, 10 Feb 2023 11:18:00 +0100 Subject: [PATCH] Helper tool to fix json_appendable files when the screen size has changed during recording --- app/fix_window_size.py | 68 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 app/fix_window_size.py diff --git a/app/fix_window_size.py b/app/fix_window_size.py new file mode 100644 index 0000000..935cd87 --- /dev/null +++ b/app/fix_window_size.py @@ -0,0 +1,68 @@ +""" +Some recordings start with the wrong window size, probably because they were started on the laptop screen. +This script modifies the opening dimensions and all subsequent viewbox elements to adhere to the new size. +""" + +import json +import logging +import argparse +import sys + +# TODO the whole script (still a copy) + +logger = logging.getLogger("svganim.fixer") +argParser = argparse.ArgumentParser( + description="""Helper tool to fix the window size when the recording started with a smaller screen that full screen. + + Beware, when used with a shell stdin redirect, do not point it towards the input file. That file will be truncated before reading! + """ +) +argParser.add_argument( + "--input", type=str, help="Filename to run on (.json_appendable)" +) +argParser.add_argument( + "--width", type=int, help="New width in px" +) +argParser.add_argument( + "--height", type=int, help="New height in px" +) +args = argParser.parse_args() + + +with open(args.input, "r") as fp: + events = json.loads("[" + fp.read() + "]") + extrabox = None + for i, event in enumerate(events): + if type(event) is list: + # if event[1] != args.width or event[2] != args.height: + # # correct default position + # # extrabox = {"event": "viewbox", "viewboxes": [{ + # # 't': 0, + # # 'x': (event[1] - args.width)/2, + # # 'y': (event[2] - args.height)/2, + # # 'width': args.width, + # # 'height': args.height, + # # }], 'original_size': [event[1], event[2]]} + event[1] = args.width + event[2] = args.height + elif event["event"] == "viewbox": + for key, box in enumerate(event['viewboxes']): + # {"t": 1088912, "x": 0, "y": 0, "width": 2048, "height": 1152}, + box['x'] += (box['width'] - args.width)/2 + box['y'] += (box['height'] - args.height)/2 + box['width'] = args.width + box['height'] = args.height + event['viewboxes'][key] = box + # elif event["event"] == "stroke": + # event['points'] = [[p[0], p[1], p[2], p[3] + time_offset] + # for p in event['points']] + + if i>0: + sys.stdout.write(",\n") + sys.stdout.write(json.dumps(event)) + if extrabox: + sys.stdout.write(",\n") + sys.stdout.write(json.dumps(extrabox)) + extrabox = None + +