473,408 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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

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()
Jun 29 '08 #1
4 2081
Lie
On Jun 29, 11:18*am, la...@portcommodore.com wrote:
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 *
DON'T DO THAT...

You're importing everything to the current namespace and this corrupts
the current namespace, specifically the 'open' function inside
Image.open would shadow the built-in 'open' function.

use:
import Tkinter
import string
import Image

There are use cases where doing 'from blah import *' is useful, such
as importing constants, but in general try to avoid importing
everything to current namespace.
root = Tk()
root.title('Canvas')
If you used 'import Tkinter', you'd have to change that code to:
root = Tkinter.Tk()
#open commodore Cset rom
cset *= open("chargen","r")
Because you shadowed the built-in 'open' with the 'from Image import
*', this would call Image.open instead of the built-in open.
canvas = Canvas(width=16, height=16, bg='white')
If you used 'import Tkinter', you'd have to change that code to:
canvas = Tkinter.Canvas(...)
canvas.pack(expand=YES, fill=BOTH)

# character size factor
size = 2

# read all 512 characters from ROM
for cchar in range(0, 511, 1):
You can use this instead:
for cchar in range(511):

but beware, this creates range with length 511 (so do the original
range), which means you're lacking on space for the last char.
You probably wanted this instead:
for cchar in range(512):

But again, python can loop directly over string/list/file, etc, so
this might be best:
for char in cset.read():
* * #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
Since you're planning to use Image module (from PIL/Python Imaging
Library) why not use functions from Image instead to create the image.
The format of the file you're using seems to be RAW format (i.e.
simple uncompressed bitmap, without any kinds of header). That means
Image.fromstring() should work.
* * * * 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()
Jun 29 '08 #2
Lie
On Jun 29, 4:47*pm, Lie <Lie.1...@gmail.comwrote:
On Jun 29, 11:18*am, la...@portcommodore.com wrote:
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 *

DON'T DO THAT...

You're importing everything to the current namespace and this corrupts
the current namespace, specifically the 'open' function inside
Image.open would shadow the built-in 'open' function.

use:
import Tkinter
import string
import Image

There are use cases where doing 'from blah import *' is useful, such
as importing constants, but in general try to avoid importing
everything to current namespace.
root = Tk()
root.title('Canvas')

If you used 'import Tkinter', you'd have to change that code to:
root = Tkinter.Tk()
#open commodore Cset rom
cset *= open("chargen","r")

Because you shadowed the built-in 'open' with the 'from Image import
*', this would call Image.open instead of the built-in open.
canvas = Canvas(width=16, height=16, bg='white')

If you used 'import Tkinter', you'd have to change that code to:
canvas = Tkinter.Canvas(...)
canvas.pack(expand=YES, fill=BOTH)
# character size factor
size = 2
# read all 512 characters from ROM
for cchar in range(0, 511, 1):

You can use this instead:
for cchar in range(511):

but beware, this creates range with length 511 (so do the original
range), which means you're lacking on space for the last char.
You probably wanted this instead:
for cchar in range(512):

But again, python can loop directly over string/list/file, etc, so
this might be best:
for char in cset.read():
* * #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

Since you're planning to use Image module (from PIL/Python Imaging
Library) why not use functions from Image instead to create the image.
The format of the file you're using seems to be RAW format (i.e.
simple uncompressed bitmap, without any kinds of header). That means
Image.fromstring() should work.
* * * * 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()

btw, PIL Handbook is a good tutorial/reference for Python Imaging
Library: http://www.pythonware.com/library/pi...book/index.htm
for info on raw mode: http://www.pythonware.com/library/pi...ok/decoder.htm
Jun 29 '08 #3
Wonderful, thank you! Will try them out this evening.

The image module syntax looks more like what I was expecting than
TKinter. All the online drawing examples I found yesterday used
TKinter; image was only shown to manipulate pre-made images.

Larry
Jun 29 '08 #4
success, had to fill in a few blanks with some more googling, here is
the finished script (used all for loops this time, saved a few more
lines):

==========

#!/usr/local/bin/python

import string
import Image, ImageDraw

size = 2

im = Image.new("1",[8*size,8*size],1)
draw = ImageDraw.Draw(im)

cset = open("chargen","r")
for cchar in range(0, 512, 1):
for charline in range(0, 8, 1):
x = cset.read(1)
ch = ord(x)
for position in range(0, 8, 1):
if ch & ( 2 ** position ):
xp = (7-position)*size
yp = charline*size
draw.rectangle(((xp,yp),(xp+size-1,yp+size-1)),
fill=0 )
outfile = "/home/mydir/work/char"+string.zfill(cchar,3)+".png"
im.save(outfile,"png")
draw.rectangle(((0,0),(size*8,size*8)),fill=1)

im.show()

==========

It does the variable sizes like I wanted and it now sure is fast.

If I wanted to display an image without saving how do I do that, on
the image module it does not pop up a canvas.. the im.show() on the
bottom does not seem to work.

Thanks again!
Jun 30 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: matt | last post by:
I have compiled some code, some written by me, some compiled from various sources online, and basically i've got a very simple flat file photo gallery. An upload form, to upload the photos and give...
5
by: john | last post by:
Here is the short story of what i'm trying to do. I have a 4 sided case labeling printer setting out on one of our production lines. Now then i have a vb.net application that sends data to this...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
4
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working,...
2
by: Chris Millar | last post by:
Can anyone help me on converting this vb asp page to C#, thanks in advance. chris. <!DOCTYPE HTML PUBLIC "-//W3C//Dtd HTML 4.0 transitional//EN"> <%...
3
by: alyssa | last post by:
Hi guys, May i know how to declare a string of binary data and pass it to the method? For example, int a={10010001120420052314} is it correct? and if i have receive the binary data, may i know...
4
by: Beginner | last post by:
How do I convert JPEG images to binary files in ASP.NET? Please advice. Thanks.
3
by: Howler | last post by:
Hello all, I am having a hard time seeing what I am doing wrong with a program I am having to write that converts pbm monochrome images into a similar pgm file. The problem I am having is...
6
by: Iron Blood | last post by:
#!/usr/bin/perl -w use strict; print "This program will convert Decimal to Binary \n"; print "Please pick your Decimal Number \n"; # Get the number from the command line or use default. my...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.