Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_canvas_zzz.py ZZZ
- import math
- from Tkinter import *
- try:
- import Image, ImageTk
- except:
- try:
- from PIL import Image, ImageTk
- except:
- raise Error("PIL not installed")
- def mouseDown(event):
- x0 = int(canvas.canvasx(event.x))
- y0 = int(canvas.canvasx(event.y))
- mousecoords[:] = [x0, y0]
- def mouseUp(event):
- canvas.delete("all")
- canvas.config(bg='black')
- X0, Y0 = mousecoords
- X1 = int(canvas.canvasx(event.x))
- Y1 = int(canvas.canvasx(event.y))
- mousecoords[:] = [X0, Y0, X1, Y1]
- canvas.create_rectangle(mousecoords, outline='white')
- canvas.update()
- print mousecoords
- def isBox():
- return len(mousecoords)==4
- def olddisplay(array, site=None):
- if site is not None:
- im.putpixel(site, grayscale(array[site]))
- else:
- for i in range(array.size[0]):
- for j in range(array.size[1]):
- im.putpixel((i,j), grayscale(array[i,j]))
- displayIm = im.resize(size)
- tkimage.paste(displayIm)
- canvas.update()
- def fastdisplay(array):
- scaleddata=simpgrayscale(array.flatten())
- im.putdata(scaleddata)
- displayIm = im.resize(size)
- tkimage.paste(displayIm)
- canvas.update()
- def display(array, site=None):
- if site is not None:
- im.putpixel(site, grayscale(array[site]))
- else:
- scaleddata=simpgrayscale(array.transpose().flatten())
- im.putdata(scaleddata)
- displayIm = im.resize(size)
- tkimage.paste(displayIm)
- canvas.update()
- def simpgrayscale(value):
- sval = (value-zmin)/(zrange)
- sval=min(sval,1)
- sval=max(sval,0)
- sval=255.*(sval)
- return sval.astype(int)
- def grayscale(value):
- sval = (value-zmin)/(zrange)
- if sval < 0.: sval = 0.
- if sval > 1.: sval = 1.
- return int(255.*(sval))
- DefaultImageSize = (400,400)
- size=DefaultImageSize
- mode='P'
- zmin=0.0
- zmax=1.0
- mousecoords = []
- root = Tk()
- root.withdraw()
- top = Toplevel()
- top.title('test')
- Label(top)
- canvas = Canvas(top,
- width=size[0], height=size[1])
- Widget.bind(canvas, "<Button-1>",
- mouseDown)
- Widget.bind(canvas, "<Button1-ButtonRelease>",
- mouseUp)
- im = Image.new(mode, size)
- zrange = zmax-zmin
- displayIm = im.resize(size)
- #displayIm = im
- if mode == '1':
- tkimage = \
- ImageTk.BitmapImage(displayIm,
- foreground="white")
- else:
- tkimage = \
- ImageTk.PhotoImage(displayIm)
- canvas.create_image(0, 0, anchor=NW, image=tkimage)
- canvas.pack()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement