1#!/usr/bin/env python3 2 3# retro-embedimage 4# 5# This takes an ngaImage and generates a version that provides the 6# image as a Python list. Output is written to stdout. 7# 8# Copyright (c) 2020, Charles Childers 9# Copyright (c) 2021, Arland Childers 10# 11# Usage: 12# 13# retro-embedimage.py [image] 14 15 16import os, sys, struct 17from struct import pack, unpack 18 19 20def prints(length, priv, end=", "): 21 if priv != None: 22 if length == 1: 23 print(priv, end=end) 24 else: 25 print("[{},{}]".format(length, priv), end=end) 26 27 28if __name__ == "__main__": 29 cells = int(os.path.getsize(sys.argv[1]) / 4) 30 f = open(sys.argv[1], "rb") 31 memory = list(struct.unpack(cells * "i", f.read())) 32 f.close() 33 count = -1 # This is counts for the extra loop at the beginning 34 print("InitialImage = [", end="\n ") 35 length = 1 36 priv = None 37 38 for cell in memory: 39 if cell == priv: 40 length += 1 41 else: 42 prints(length, priv) 43 priv = cell 44 length = 1 45 count += 1 46 if count >= 10: 47 print(end="\n ") 48 count = 0 49 prints(length, priv, end="") 50 print("\n]") 51