65X-DOS/bin/makechrom

53 lines
1.2 KiB
Python
Executable File

#!/usr/bin/env python3
import sys, traceback
from PIL import Image
flip_d = False
if len(sys.argv)<2:
print('ERROR: Please specify at least one file to convert!')
print('Note: character size is calculated based on resolution assuming 16x16 grid.\n')
for i in range(len(sys.argv)-1):
if sys.argv[i+1][:1]!='-':
continue
if sys.argv[i+1]=='-f':
flip_d = True
for i in range(len(sys.argv)-1):
if sys.argv[i+1][:1]=='-':
continue
try:
print(f'Processing "{sys.argv[i+1]}"...')
img = Image.open(sys.argv[i+1])
width, height = img.size
cw = width>>4
ch = height>>4
print(f'({width},{height}) => char({cw},{ch})')
pixels = img.load()
f = open(f'{sys.argv[i+1]}.bin','wb')
ba = bytearray()
for y in range(16):
for x in range(16):
for py in range(ch):
outbyte = 0
for px in range(cw):
if flip_d:
outbyte = outbyte << 1
if pixels[(x*cw)+px,(y*ch)+py]>0:
if flip_d:
outbyte = outbyte | 1
else:
outbyte = outbyte | (1<<(px&7))
if (px&7)==7:
ba.append(outbyte)
outbyte=0
f.write(ba)
f.close()
except Exception as e:
print(f'Exception caught!\n"{traceback.format_exc()}"')
print('Task completed.\n')