All pastes #238520 Raw Edit

Miscellany

public python v1 · immutable
·published 2006-11-05 00:44 UTC
rendered paste body
#!/usr/bin/python# -*- coding: iso-8859-1# Q:      How many lawyers does it take to change a light bulb?# A:      One.  Only it's his light bulb when he's done.#from Numeric import zerosfrom Tkinter import *def fancyprint(image, to=sys.stdout, chars=".X?", double=False):  '''Gibt Zeile f. Zeile, Zeichen f. Zeichen die Matrix aus.'''  for i in image:    line=""    for j in i:      if j > 0 :        line += chars[0]      elif j < 0:        line += chars[1]      else:        line += chars[2]    print >>to, line    if double: # Zweizeilig, fuer die Folie      line=""      for j in i:        if j > 0 :          line += chars[3]        elif j < 0:          line += chars[4]        else:          line += chars[5]      print >>to, linedef savefax():  '''Speichert ein entschlüsseltes Fax als .pbm.'''  f=open(names[curfax][:-4]+"_plain.pbm",'w')  f.write("P1\n")  print >>f, xlim, ylim  fancyprint(image[curfax]*folie,f,"011")  f.close()def saverawfaxes():  '''Speichert ein entschlüsseltes Fax, aber behält unbestimmte Pixel bei.'''  for faxnr in xrange(0,len(image)):    f=open(names[faxnr][:-4]+"_auto",'w')    f.write("P2\n")    print >>f, xlim, ylim    fancyprint(image[faxnr]*folie,f,"012")    f.close()def savefolie():  '''Speichert die Entschlüsselungsfolie.'''  f=open("decrypt_folie.pbm",'w')  f.write("P1\n")  print >>f, xlim*2, ylim*2  fancyprint(folie, f, [ "10", "01", "11",\                         "01", "10", "11" ] , True)  f.close()def normalize(array2d):  '''Setzt die Elemente eines Arrays auf -1, 0, oder 1'''  for i in xrange(len(array2d)):    for j in xrange(len(array2d[0])):      array2d[i][j]=cmp(array2d[i][j],0)image=[]names=[]if sys.argv[1] == '-r':  SaveRawFaxes=True  del sys.argv[1]else:  SaveRawFaxes=Falsefor cryptfax in sys.argv[1:]:  f=open(cryptfax)  if f.readline() != "P1\n":    print >>sys.stderr, "Fehler:", cryptfax, \      "ist kein unterstuetztes .pbm Bild."    sys.exit()  coords= f.readline().split()  xlim=int(coords[0])/2  ylim=int(coords[1])/2  image.append(zeros((ylim,xlim)))  names.append(cryptfax)  i=j=0  flip=1  for line in f:    for char in line:      if char == "0":        image[-1][j//2][i//2] -= flip      elif char == "1":        image[-1][j//2][i//2] += flip      else: continue      i += 1      flip *= -1 # Der zweite Pixel wird andersherum gewertet      if i == xlim*2:        i=0        j += 1        flip *= -1 # Der erste auf der naechsten Zeile wieder  normalize(image[-1])  f.closefolie=zeros((ylim,xlim))for fax in image:  folie += faxnormalize(folie)# (vorlaeufige) Entschluesselungsfolie ist schon fertig# ================== tkinter face ===========def drawfax(fax):  global X,Y  c.delete(ALL)  c.create_rectangle(0,0,xlim,ylim)  X=c.winfo_width()  Y=c.winfo_height()  Xsize=X//xlim  Ysize=Y//ylim  for i in xrange(xlim):    for j in xrange(ylim):      colr=cmp(fax[j][i],0)*127+128      c.create_rectangle(i*Xsize,j*Ysize,(i+1)*Xsize,(j+1)*Ysize,\                         fill='#%02X%02X%02X'%(colr,colr,colr))def nextfax():  global curfax  curfax += 1  if curfax == len(image):    curfax=0  redraw()def prevfax():  global curfax  curfax -= 1  if curfax < 0:    curfax = len(image)-1  redraw()def redraw():  drawfax(image[curfax]*folie)  root.title(names[curfax])def reszcheck():  '''Zeichnet das Fenster neu, wenn die Größe geändert wurde.  Dies ist nötig, damit nach z.B. dem Maximieren das Bild wieder in angemessener  Größe angezeigt wird.'''  global X, Y  if X != c.winfo_width() or Y != c.winfo_height():    redraw()  c.after(100,reszcheck)def klickhandle(event):  X=c.winfo_width()//xlim*xlim  Y=c.winfo_height()//ylim*ylim  pixX=event.x*xlim//X  pixY=event.y*ylim//Y  if event.num == 3:    folie[pixY][pixX] = -1 * image[curfax][pixY][pixX]  elif event.num == 2:    folie[pixY][pixX] = image[curfax][pixY][pixX]  else:    if folie[pixY][pixX] < 0:      folie[pixY][pixX] = 1    else:      folie[pixY][pixX] = -1  redraw()def showfaxes():  global c,curfax,root  curfax=0  root=Tk()  topframe=Frame()  midframe=Frame()  Button(topframe,text="Save Folie",command=savefolie).pack(side=LEFT)  Button(topframe,text="Exit",command=sys.exit).pack(side=LEFT)  Button(midframe,text="Previous",command=prevfax).pack(side=LEFT)  Button(midframe,text="Save",command=savefax).pack(side=LEFT)  Button(midframe,text="Next",command=nextfax).pack(side=LEFT)  topframe.pack()  midframe.pack()  c=Canvas(root)  c.pack(side=BOTTOM, fill=BOTH, expand=1)  c.after(100,redraw)  c.after(500,reszcheck)  c.bind('<Button-1>', klickhandle)  c.bind('<Button-2>', klickhandle)  c.bind('<Button-3>', klickhandle)  root.mainloop()# ===========================================if SaveRawFaxes:  saverawfaxes()try:  showfaxes()except TclError: # Keine grafische Oberflaeche, einfach als Text ausgeben  for faxnr in xrange(0,len(image)):    print ""    print "Fax", names[faxnr], "ist:"    fancyprint(image[faxnr]*folie)    print ""