Image to Spectrum TZX script
From CiaransWiki
The following is my hacky Python script referred to in this post.
specgen.py
import struct
import operator
import Image
outfile="specgen.tzx"
infile="test.jpg"
#We'll store the screen and attributes in seperate arrays
screen=[]
attrs=[]
#Load the image and write it to our 'screen':
img=Image.open(infile)
img=img.resize((256,192))
img=img.convert('1')
img.save("test2.png")
curbyte=0
curbit=7
for i in img.getdata():
if i==0:
curbyte|=1<<curbit
curbit-=1
if curbit<0:
curbit=7
screen.append(curbyte)
curbyte=0
#Set all the attributes:
for i in range(32*24):
attrs.append(7*16)
#Generate header block
header=struct.pack('B',0)
header+=struct.pack('B',3)
header+='Specgen '
header+=struct.pack('HHH',0x1B00,0x4000,0x8000)
checksum=0
for c in header:
checksum^=ord(c)
header+=struct.pack('B',checksum)
#Generate data block
data=struct.pack('B',0xFF)
for third in [0,0x800,0x1000]:
for rowstart in [0,32,64,96,128,160,192,224]:
for charrow in range(8):
for i in screen[third+charrow*32*8+rowstart:third+charrow*32*8+rowstart+32]:
data+=struct.pack('B',i)
for i in attrs:
data+=struct.pack('B',i)
checksum=0
for c in data:
checksum^=ord(c)
data+=struct.pack('B',checksum)
#Write the whole lot to a TZX file...
tzx=open(outfile,'wb')
#Write the TZX header...
tzx.write('ZXTape!')
tzx.write(struct.pack('BBB',0x1A,1,20))
#Write the block for the tape header...
tzx.write(struct.pack('B',0x10))
tzx.write(struct.pack('H',1000))
tzx.write(struct.pack('H',len(header)))
tzx.write(header)
#Write the main data block...
tzx.write(struct.pack('B',0x10))
tzx.write(struct.pack('H',1000))
tzx.write(struct.pack('H',len(data)))
tzx.write(data)
tzx.close()

