Skip to content

Instantly share code, notes, and snippets.

@diegotf30
Last active August 7, 2018 21:59
Show Gist options
  • Save diegotf30/54c51ebad12ad90db19c365df0972392 to your computer and use it in GitHub Desktop.
Save diegotf30/54c51ebad12ad90db19c365df0972392 to your computer and use it in GitHub Desktop.
Transforms sizes.txt to JSON format, used in Meme-bot repo
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