Last active
August 7, 2018 21:59
-
-
Save diegotf30/54c51ebad12ad90db19c365df0972392 to your computer and use it in GitHub Desktop.
Transforms sizes.txt to JSON format, used in Meme-bot repo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
def tuple2str(str1, str2) : | |
return '(' + str1 + ',' + str2 + ')' | |
jsonData = {} | |
file = open('sizes.txt') | |
file.readline() # Ignore header | |
for line in file: | |
boxes = line.split() | |
jsonLine = {"background": template_boxes[1], "boxes": []} | |
box_no = 2 # Skips Template Num. and bg_color | |
while box_no < len(boxes) : | |
current_box = {} | |
if boxes[box_no] == 's': | |
current_box['repeat_prev'] = True | |
box_no += 1 | |
current_box['size'] = tuple2str(boxes[box_no], boxes[box_no + 1]) | |
current_box['left_corner'] = tuple2str(boxes[box_no + 2], boxes[box_no + 3]) | |
jsonLine['boxes'].append(current_box) | |
box_no += 4 | |
jsonData[boxes[0]] = jsonLine | |
with open('sizes.json', 'w') as output: | |
json.dump(jsonData, output, sort_keys = False, indent = 4, | |
ensure_ascii = True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment