## creates ASCII random character stereograms ## 03/2006 Wolfgang.Urban@schule.at ## ## example: ## >>> stereogram("image.gif") ## where image is a depth file approx 70x30 pixels for screen viewing. ## Adjust sizes and font sizes (courier type) to fit your needs. ## ## Have fun. import random class AsciiStereogram: ## setup character sets def __init__(self,patternwidth=10): self.patternwidth = patternwidth self.textchars = " ABCDEFGHIJKLMNOPQRSTUVWXYZ" self.fillchars = "123456789abcdefghijklmnopq" self.output = "" self.line = "" ## start a new line def newline(self): if self.line: self.output += self.line+"\n" t = list(self.textchars[:]) random.shuffle(t) self.pattern = "".join(t[:self.patternwidth]) f = list(self.fillchars[:]) random.shuffle(f) self.fill = "".join(f) self.line = self.pattern[:] # start with a pattern self.depth = 0 ## add one char representing depth def add(self,depth): if depth > self.depth: # go to front, shorten pattern self.pattern = self.pattern[(depth-self.depth):] self.depth = depth elif depth < self.depth: # go sdeeper, lengthen pattern self.pattern = self.fill[:(self.depth-depth)]+self.pattern self.fill = self.fill[(self.depth-depth):] self.depth = depth ## append first pattern char and recycle it for later use self.line += self.pattern[0] self.pattern = self.pattern[1:]+self.pattern[0] ## return whole text including last line def get(self): return self.output+self.line+"\n" ## two circles as a focussing aid def focusline(self,n): n += self.patternwidth mid = " "*((n-self.patternwidth)/2)+"O"+" "*(self.patternwidth-2)+"O" return mid ## use given text as pattern in lines class AsciTextogram(AsciiStereogram): def __init__(self): AsciiStereogram.__init__(self) text = ["erstes","zweites","drittes","viertes","fünftes"] ############################################################################## def stereogram(name="ellipse4.gif"): depthimage = loaddepthimage(name) a = AsciiStereogram() for line in depthimage: a.newline() for d in line: a.add(int(d)) stereo = a.focusline(70)+"\n"+a.get() # print stereo ## text output in IDLE savedepth(depthimage,"depth.txt") # save depth as text savestereogram(stereo,"stereo.txt") # save stereogram as text # show text in a Tk Window s = TkStereoText() s.write(stereo,0,0) from Tkinter import * ## load a gif as a depth image def loaddepthimage(name): root = Tk() pic = PhotoImage(file=name) t = [] for y in range(pic.height()): line = "" for x in range(pic.width()): h = pic.get(x,y).split()[0] line += str(int(h)/64) t.append(line) root.destroy() return t ## save depth image as ascii-file def savedepth(depth,name="depth.txt"): f = open(name,"w") for y in depth: f.write(y+"\n") f.close() ## save stereogram as ascii-file def savestereogram(pic,name="stereo.txt"): f = open(name,"w") f.write(pic) f.close() class TkStereoText: def __init__(self): self.root = Tk() self.c = Canvas(self.root, width=880+6,height=660+6) self.c.pack() def write(self,text,x,y): font = ("Courier",10,"bold") self.c.create_text(x+440+3,y+330+3,text=text,font=font) self.c.update()