Skip to content

Instantly share code, notes, and snippets.

@boq
Created November 18, 2018 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boq/70cad746383bfc23eed82c616d9d3255 to your computer and use it in GitHub Desktop.
Save boq/70cad746383bfc23eed82c616d9d3255 to your computer and use it in GitHub Desktop.
from __future__ import division
from PIL import Image, ImageMath
MC_CHARS = u"\u00c0\u00c1\u00c2\u00c8\u00ca\u00cb\u00cd\u00d3\u00d4\u00d5\u00da\u00df\u00e3\u00f5\u011f\u0130" + \
u"\u0131\u0152\u0153\u015e\u015f\u0174\u0175\u017e\u0207\u0000\u0000\u0000\u0000\u0000\u0000\u0000" + \
u"\u0020\u0021\"\u0023\u0024\u0025\u0026\u0027\u0028\u0029\u002a\u002b\u002c\u002d\u002e\u002f" + \
u"\u0030\u0031\u0032\u0033\u0034\u0035\u0036\u0037\u0038\u0039\u003a\u003b\u003c\u003d\u003e\u003f" + \
u"\u0040\u0041\u0042\u0043\u0044\u0045\u0046\u0047\u0048\u0049\u004a\u004b\u004c\u004d\u004e\u004f" + \
u"\u0050\u0051\u0052\u0053\u0054\u0055\u0056\u0057\u0058\u0059\u005a\u005b\\\u005d\u005e\u005f" + \
u"\u0060\u0061\u0062\u0063\u0064\u0065\u0066\u0067\u0068\u0069\u006a\u006b\u006c\u006d\u006e\u006f" + \
u"\u0070\u0071\u0072\u0073\u0074\u0075\u0076\u0077\u0078\u0079\u007a\u007b\u007c\u007d\u007e\u0000" + \
u"\u00c7\u00fc\u00e9\u00e2\u00e4\u00e0\u00e5\u00e7\u00ea\u00eb\u00e8\u00ef\u00ee\u00ec\u00c4\u00c5" + \
u"\u00c9\u00e6\u00c6\u00f4\u00f6\u00f2\u00fb\u00f9\u00ff\u00d6\u00dc\u00f8\u00a3\u00d8\u00d7\u0192" + \
u"\u00e1\u00ed\u00f3\u00fa\u00f1\u00d1\u00aa\u00ba\u00bf\u00ae\u00ac\u00bd\u00bc\u00a1\u00ab\u00bb" + \
u"\u2591\u2592\u2593\u2502\u2524\u2561\u2562\u2556\u2555\u2563\u2551\u2557\u255d\u255c\u255b\u2510" + \
u"\u2514\u2534\u252c\u251c\u2500\u253c\u255e\u255f\u255a\u2554\u2569\u2566\u2560\u2550\u256c\u2567" + \
u"\u2568\u2564\u2565\u2559\u2558\u2552\u2553\u256b\u256a\u2518\u250c\u2588\u2584\u258c\u2590\u2580" + \
u"\u03b1\u03b2\u0393\u03c0\u03a3\u03c3\u03bc\u03c4\u03a6\u0398\u03a9\u03b4\u221e\u2205\u2208\u2229" + \
u"\u2261\u00b1\u2265\u2264\u2320\u2321\u00f7\u2248\u00b0\u2219\u00b7\u221a\u207f\u00b2\u25a0\u0000"
BYTE_COUNT = 4096
LINE_WIDTH = 64
PIXEL_COUNT = BYTE_COUNT * 8
ASCII_TO_MC = dict(map(lambda (k,v): (ord(v),k), enumerate(MC_CHARS)))
with open("c64.bin", "rb") as input:
bits = input.read(BYTE_COUNT)
with open("c64.map", "rb") as input:
sheet_map = input.read(4096)
chars = {}
ptr = 0
ch = 0
while ptr < BYTE_COUNT:
c = bits[ptr:ptr+8]
ascii_ch = sheet_map[ch]
if ascii_ch:
mc_ch = ASCII_TO_MC[ord(ascii_ch)]
chars[mc_ch] = c
ptr += 8
ch += 1
patch = ""
mask = ""
CHARS_PER_LINE = 16
IMG_WIDTH = CHARS_PER_LINE * 8
EMPTY = "\x00" * 8
FULL = "\xFF" * 8
line_start = 0
while line_start < len(MC_CHARS):
for i in range(8):
for line_offset in range(CHARS_PER_LINE):
index = line_start + line_offset
if index in chars:
patch += chars[index][i]
mask += "\xFF"
else:
patch += "\x00"
mask += "\x00"
line_start += CHARS_PER_LINE
original = Image.open("ascii.png")
pf = Image.frombytes("1", (128, 128), patch)
pf.save("patch.png")
mf = Image.frombytes("1", (128, 128), mask)
mf.save("mask.png")
original.paste(0x00, None, mf)
original.paste(-1, None, pf)
original.save("output.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment