help debugging noob code - converting binary data to images...

larry at portcommodore.com larry at portcommodore.com
Sun Jun 29 00:18:30 EDT 2008


Ok I'm a Python noob, been doing OK so far, working on a data
conversion program and want to create some character image files from
an 8-bit ROM file.

Creating the image I've got down, I open the file and use TK to draw
the images... but

1)  It does not seem to end (running in IDLE), I have to kill the
process to retry it seems tkinter does not close(?)

2) Once I added the Image module open won't open my binary file
(complains its not an image file, which is isnt.)  I am sure I need to
prefix open with something but I can't seem to find an example of how
to word it,

Below is the code (if it is lousy its because I've mainly been
borrowing by examples as I go...) Any suggestions are gretly
appreciated.

#!/usr/local/bin/python

from Tkinter import *
from string import *
from Image import *

root = Tk()
root.title('Canvas')

#open commodore Cset rom
cset  = open("chargen","r")

canvas = Canvas(width=16, height=16, bg='white')
canvas.pack(expand=YES, fill=BOTH)

# character size factor
size = 2

# read all 512 characters from ROM
for cchar in range(0, 511, 1):
    #draw line
    while charline < 8:
        position = 0
        x = cset.read(1)
        ch = ord(x)
        # draw pixels
        while position < 8:
            if ch & ( 2 ** position ):
                xp = 1+(7-position)*size
                yp = 1+charline*size
                canvas.create_rectangle(xp,yp,xp+size,yp+size,
fill='black', width=0)
            position += 1
        charline += 1
    #save character image
    outfile = "/home/mydir/work/char"+zfill(cchar,3)+".png"
    canvas.save(outfile,"png")
    #clear canvas for next char...
    canvas.create_rectangle(1,1,size*8,size*8, fill='white', width=0)
root.mainloop()



More information about the Python-list mailing list