title: How to draw a line on image and retrieve pixels beneath the line?
body: <p>I am trying to draw a line on the image and retrieve all the pixel values that fall beneath the line. I have tried but a method where you retrieve the 2 mouse click coordinates and calculate the slope between the two mouse coordinates but that is very specifically extracting only one row. How can I change the width of the line and also retrieve the pixels beneath it? I am new to python and I don't know where to start. Any ideas or any recommendations would be great thank you.</p>
  <p>Simply to understand I want to draw a line on image and extract pixels beneath the line, and also when we change the width of line I want to extract pixel values of the extended with too. How can I do this?</p>
  <pre><code>from tkinter import *
  import PIL
  import PIL.Image 
  import PIL.ImageTk 
  root = Tk()
  
  #setting up a tkinter canvas with scrollbars
  frame = Frame(root, bd=2, relief=SUNKEN)
  frame.grid_rowconfigure(0, weight=1)
  frame.grid_columnconfigure(0, weight=1)
  xscroll = Scrollbar(frame, orient=HORIZONTAL)
  xscroll.grid(row=1, column=0, sticky=E+W)
  yscroll = Scrollbar(frame)
  yscroll.grid(row=0, column=1, sticky=N+S)
  canvas = Canvas(frame, bd=0, xscrollcommand=xscroll.set, yscrollcommand=yscroll.set)
  canvas.grid(row=0, column=0, sticky=N+S+E+W)
  xscroll.config(command=canvas.xview)
  yscroll.config(command=canvas.yview)
  frame.pack(fill=BOTH,expand=1)
  
  #adding the image in the below line u16 us my image array.
  img = PIL.ImageTk.PhotoImage(image =PIL.Image.fromarray(u16))
  
  canvas.create_image(0,0,image=img,anchor=&quot;nw&quot;)
  canvas.config(scrollregion=canvas.bbox(ALL))
  
  #function to be called when mouse is clicked
  def printcoords(event):
      #outputting x and y coords to console
      click = event.x, event.y
      print (click)
  def printcoords_up(event):
      #outputting x and y coords to console
      rem_click=event.x, event.y
      print (rem_click)
  #mouseclick event
  canvas.bind(&quot;&lt;ButtonPress-1&gt;&quot;,printcoords)
  # canvas.bind(&quot;&lt;ButtonMotion-1&gt;&quot;, printcoords)
  canvas.bind(&quot;&lt;ButtonRelease-1&gt;&quot;, printcoords_up )
  
  root.mainloop()
  </code></pre>
  <p>##################EDITED CODE #####################</p>
  <pre><code>import tkinter 
  import numpy as np
  import cv2
  import sys
  import PIL
  import PIL.Image 
  import PIL.ImageTk as imtk
  if &quot;Tkinter&quot; not in sys.modules:
      from tkinter import *
  
  curPth = sys.path[0]
  ###### u16 is my image array.
  im = u16
  tmpPth = curPth+'/temp.png'
  
  ev = None
  
  
  def click(event, back):
      global ev, im
      if ev == None:
         ev = event
         return None
  
  # im = imgPth
  # mask = cv2.cvtColor(im.copy()*0, cv2.COLOR_BGR2GRAY)
  cv2.line(im, pt1=(ev.x, ev.y), pt2=(event.x, event.y), color=(255, 0, 0), thickness=40)
  mask = cv2.threshold(im, 127, 255, cv2.THRESH_BINARY)[1]
  out = im.copy()
  out[np.where(mask == 0)] = 255
  
  cv2.imwrite(tmpPth, out)
  back.config(file=tmpPth)
  print(event)
  ev = event
  
  window = tkinter.Tk()
  back = imtk.PhotoImage(image =PIL.Image.fromarray(im))
  my_label=Label(window, image=back)
  my_label.place(x=0, y=0)
  window.bind('&lt;Button-1&gt;', lambda event: click(event, back))
  window.mainloop()
  </code></pre>