#!/usr/bin/python3 # -*- coding: utf-8 -*- import sys from PIL import Image, ImageDraw in_file = sys.argv[1] rgb332_palette=[] #RGB palette generation: Red(3bits) Green(3bits) Blue(2bits)/1pixel = 256 colors (as used in .coe VGA mem files) for i in range(256): rgb332_palette.append(int(((i & 224) >> 5)*(255/7))) # & 224 means a bit mask for the MSB and >> 5 is a bitwise to strip LSB bits rgb332_palette.append(int(((i & 28) >> 2)*(255/7))) rgb332_palette.append(int((i & 3)*(255/3))) def coe_parse(key_name, key_end_char, separator): with open(in_file, encoding='utf-8', mode='r') as coefile: coefile_data = coefile.read() key=key_name key_offset=coefile_data.find(key) key_end=coefile_data.find(key_end_char,key_offset) #find end char starting from key place key_value=coefile_data[key_offset:key_end].split(separator)[1] #value is 2nd element in the split return key_value imgbytes = bytes.fromhex(coe_parse('memory_initialization_vector=', ';', '=').replace(',', '').replace('\n','')) height=int(coe_parse('Height:',',',' ')) width=int(coe_parse('Width:','\n',' ')) #end char = new line! img = Image.frombytes('P', (height, width), imgbytes) img.putpalette(rgb332_palette) img.show()