[Tutor] ASCII art generator
Michael Miller
blackmariah@shmups.com
Sun Feb 2 08:30:01 2003
--Boundary_(ID_ejBwXmFt1jhqmvdvQn3FVQ)
Content-type: text/plain; charset=iso-8859-1
Content-transfer-encoding: 7BIT
I got this thing working in about 4 hours time the other night (an amazing
feat for me) and have been messing with it sense. It's a very simple script
that takes the value of a pixel, changes it to the hex value, then formats in
in HTML and writes the info to a file. I'm happy with the way it works like
it is, but I'd like for it to be usable for other people too. I plan to add a
gui to it sometime soo, but need to learn to make a gui first (Walking?
Forget that, I'm all about running. Usually into trees).
The two more pressing issues are making sure that the user is using an image
file as the input and not, for instance, their grocery list (I haven't
checked to see what happens if they do). Second, I need a status indicator.
Right now all it does is sit until it's done and as slow as this thing is,
that's a problem. I was thinking of a percentage indicator based on the total
amount of pixels in the image, but I have no clue how to implement that right
now (I'll give it a shot later).
I realise that this is a rather brute force method of doing this, but I just
don't have the skill to make anything more elegant. Any idea on how to do so,
or any help with the problems I mentioned above would be most appreciated.
Also, sorry that I had to attach the file. My crappy free webhost (my good
free webhost is down ATM) won't allow Python files. Thanks for any help.
Michael Miller
--Boundary_(ID_ejBwXmFt1jhqmvdvQn3FVQ)
Content-type: text/x-python; charset=iso-8859-1; name=asciigui.py
Content-transfer-encoding: 7BIT
Content-disposition: attachment; filename=asciigui.py
##############################
# ASCII art generation script WITH GUI!x
#
# Written by Michael Miller
# blackmariah@techie.com
#
# Thanks to Magnus Lycka from the
# Python tutor mailing list for helping
# with the decimal/hex conversion.
##############################
#####################
#--- Let's import some stuff.
from dialog import *
import Image
import string
import random
####################
#--- Functions ---#
def makerand(): #-- Choose a random character to print.
mylet = list(string.printable)
foo = random.randrange(0, 93) #-- We use this range because after this is weird characters and whitespace.
return mylet[foo]
def ascgen(): #--- The fun stuff.
x = 0
y = 0
im = Image.open(file_name)
im.load()
sz = im.size
szlist = list(sz)
width = sz[0]
height = sz[1]
total = width*height
while x < width:
value = im.getpixel([x, y]) #--- Get the RGB value of the pixel at [x, y]
vallist = list(value) #--- Turn the tuple returned by im.getpixel() into a list
r = vallist[0]
g = vallist[1]
b = vallist[2]
#--- Convert the numbers in vallist into their hex equivalents.
out = '<FONT color="','#%02X%02X%02X'%(r,g,b),'">',makerand(),'</FONT>'
myfile.writelines(out) #--- Write the above line to file.
x = x+1
if x == width: #--- Checks to see if we've hit the end of a line or end of file
myfile.write("<br>")
#print y
y = y+1
x = 0
if y == height:
return
def main(): #-- The main loop. Duh. Prints the HTML portions of the file.
myfile.write("<html>")
myfile.write('<body bgcolor="black">')
myfile.write('<font face="Courier" size="-3">')
ascgen()
myfile.write("</font>")
myfile.write("</body>")
myfile.write("</html>")
return
#--- The main stuff.
print "ASCII art generator"
print "Make pretty pictures from other pictures."
print "ALMOST entirely, but not quite pointless.\n"
print "First, we need an input file."
print "Be sure to use the full path. Example: /home/foo/bar.jpg"
file_name = raw_input(">>>")
print "Now we need an output file name."
print "Remember to type the extension too: FOO.HTML"
output_file = raw_input(">>>")
myfile = open(output_file, 'a')
main()
print file_name
print "Done."
--Boundary_(ID_ejBwXmFt1jhqmvdvQn3FVQ)--