Python Forum

Full Version: drawing random over the image
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to draw randomly over the image. Image can be either .jpeg file or simple black background. Once the drawing is complete I need to save the drawing as a numpy array.
I have found a few solutions that might work but non of them work for me for some reason.
For example, one idea is to use tkinter
from tkinter import *
app = Tk()
app.mainloop
canvas = Canvas(app, bg='black')
canvas.pack(anchor='nw', fill='both', expand=1)
def get_x_and_y(event):
    global lasx, lasy
    lasx, lasy = event.x, event.y

def draw_smth(event):
    global lasx, lasy
    canvas.create_line((lasx, lasy, event.x, event.y), fill='red', width=2)
    lasx, lasy = event.x, event.y
(
)
However, when I run the codes above it just seems to "run" in the infinite loop in the first three lines. I am also note sure how to convert this drawing into numpy.

I have also tried this:
import cv2
import numpy as np 
im = cv2.imread("einstein.jpg")
cv2.namedWindow("Jenya Polyakova OpenCV")
cv2.setMouseCallback('Jenya Polyakova OpenCV',geology_draw)
while(1):
    cv2.imshow('Jenya Polyakova OpenCV',im)
    k=cv2.waitKey(1)&0xFF
    if k==27:
        break
cv2.destroyAllWindows()
Again, it just seems to go into infinite loop. I am using jypiter notebook to compile.
I also have seen some turtle solution but have not tried.
Any ideas/suggestions would be appreciated.
Thank you!