Advertisement
here2share

# Tk_canvas_zzz.py

Aug 6th, 2020
1,327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | None | 0 0
  1. # Tk_canvas_zzz.py ZZZ
  2.  
  3. import math
  4. from Tkinter import *
  5. try:
  6.     import Image, ImageTk
  7. except:
  8.     try:
  9.         from PIL import Image, ImageTk
  10.     except:
  11.         raise Error("PIL not installed")
  12.  
  13. def mouseDown(event):
  14.     x0 = int(canvas.canvasx(event.x))
  15.     y0 = int(canvas.canvasx(event.y))
  16.     mousecoords[:] = [x0, y0]
  17.  
  18. def mouseUp(event):
  19.     canvas.delete("all")
  20.     canvas.config(bg='black')
  21.     X0, Y0 = mousecoords
  22.     X1 = int(canvas.canvasx(event.x))
  23.     Y1 = int(canvas.canvasx(event.y))
  24.     mousecoords[:] = [X0, Y0, X1, Y1]
  25.     canvas.create_rectangle(mousecoords, outline='white')
  26.     canvas.update()
  27.     print mousecoords
  28.  
  29. def isBox():
  30.     return len(mousecoords)==4
  31.  
  32. def olddisplay(array, site=None):
  33.     if site is not None:
  34.         im.putpixel(site, grayscale(array[site]))
  35.     else:
  36.         for i in range(array.size[0]):
  37.             for j in range(array.size[1]):
  38.                 im.putpixel((i,j), grayscale(array[i,j]))
  39.     displayIm = im.resize(size)
  40.     tkimage.paste(displayIm)
  41.     canvas.update()
  42.    
  43. def fastdisplay(array):
  44.     scaleddata=simpgrayscale(array.flatten())
  45.     im.putdata(scaleddata)
  46.     displayIm = im.resize(size)
  47.     tkimage.paste(displayIm)
  48.     canvas.update()
  49.    
  50. def display(array, site=None):
  51.     if site is not None:
  52.         im.putpixel(site, grayscale(array[site]))
  53.     else:
  54.         scaleddata=simpgrayscale(array.transpose().flatten())
  55.         im.putdata(scaleddata)
  56.     displayIm = im.resize(size)
  57.     tkimage.paste(displayIm)
  58.     canvas.update()
  59.  
  60. def simpgrayscale(value):
  61.     sval = (value-zmin)/(zrange)
  62.     sval=min(sval,1)
  63.     sval=max(sval,0)
  64.     sval=255.*(sval)
  65.     return sval.astype(int)
  66.  
  67. def grayscale(value):
  68.     sval = (value-zmin)/(zrange)
  69.     if sval < 0.: sval = 0.
  70.     if sval > 1.: sval = 1.
  71.     return int(255.*(sval))
  72.        
  73. DefaultImageSize = (400,400)
  74. size=DefaultImageSize
  75. mode='P'
  76. zmin=0.0
  77. zmax=1.0
  78.  
  79. mousecoords = []
  80. root = Tk()
  81. root.withdraw()
  82. top = Toplevel()
  83. top.title('test')
  84. Label(top)
  85. canvas = Canvas(top,
  86.              width=size[0], height=size[1])
  87. Widget.bind(canvas, "<Button-1>",
  88.                    mouseDown)
  89. Widget.bind(canvas, "<Button1-ButtonRelease>",
  90.                    mouseUp)
  91. im = Image.new(mode, size)
  92. zrange = zmax-zmin
  93. displayIm = im.resize(size)
  94. #displayIm = im
  95. if mode == '1':
  96.     tkimage = \
  97.         ImageTk.BitmapImage(displayIm,
  98.                     foreground="white")
  99. else:
  100.     tkimage = \
  101.         ImageTk.PhotoImage(displayIm)
  102. canvas.create_image(0, 0, anchor=NW, image=tkimage)
  103. canvas.pack()
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement