convert coordinates into binary image python
Question: I made a GUI in which I can load an image from disk and convert the mouse
motion ( drawing the contours ) into coordinates. Now I need to convert the
coordinates into a binary image and I don't know how. Here is my code:

    
    
    from Tkinter import *
    from tkFileDialog import askopenfilename
    from PIL import Image, ImageTk
    import numpy as np
    import cv2
    
    class Browse_image :
     def __init__ (self,master) :
    
      frame = Frame(master)
      frame.grid(sticky=W+E+N+S)
      self.browse = Button(frame, text="Browse", command = lambda: browseim(self))
      self.browse.grid(row=13, columnspan=1)
      self.photo = PhotoImage(file="browse.png")
      self.label = Label(frame, image=self.photo)
      self.label.grid(row=1,rowspan=10)
      self.label.bind('<B1-Motion>', self.mouseevent)
     def mouseevent(self,event):
       w=self.photo.width()
       h=self.photo.height()
       a = np.zeros(shape=(h,w))
       #print event.x, event.y
       a[event.x,event.y]=1
    

plt.imsave('binary.png', a, cmap=cm.gray)

    
    
    def browseim(self):
      path = askopenfilename(filetypes=(("png files","*.png"),("jpeg files",
       "*.jpeg")) )
      if path:
        img = Image.open(path)
        self.photo = ImageTk.PhotoImage(img)                    
        self.label.configure(image = self.photo)
       #self.label.image = self.photo
    
    
    root= Tk()
    b= Browse_image(root)
    root.mainloop()
    

`


Answer: Your issue is that you are creating a new empty array at each mouse event with
the line `a = np.zeros(shape=(h,w))` in mouseevent. To fix that you should
have `a` declared in the `__init__` as an attribute (ie `self.a =
np.zeros(shape=(h,w))`) so that you can acces and update it without
overwriting it in your `mouseevent` function.

