Skip to content

Instantly share code, notes, and snippets.

@palaashatri
Created August 12, 2020 05:52
Show Gist options
  • Save palaashatri/2bfbca0d585e4501f982e549d70b7e17 to your computer and use it in GitHub Desktop.
Save palaashatri/2bfbca0d585e4501f982e549d70b7e17 to your computer and use it in GitHub Desktop.
A simple drawing app made on OpenCV on Python3
import numpy as np
import cv2
# Global Variables
canvas = np.ones([500,500,3],'uint8')*255
radius = 3
color = (0,255,0)
pressed = False
# click callback
def click(event,x,y,flags,param):
global canvas,pressed
if event == cv2.EVENT_LBUTTONDOWN:
pressed = True
cv2.circle(canvas,(x,y),radius,color,-1)
if event == cv2.EVENT_MOUSEMOVE and pressed == True:
cv2.circle(canvas,(x,y),radius,color,-1)
if event == cv2.EVENT_LBUTTONUP:
pressed = False
# Window initialization and callback assignmment
cv2.namedWindow("canvas")
cv2.setMouseCallback("canvas",click)
# Forever draw loop
while True:
cv2.imshow("canvas",canvas)
# key capture every 1 ms
ch = cv2.waitKey(1)
if ch & 0xFF == ord('q'):
break
# Change colors
elif ch & 0xFF == ord('b'):
color = (255,0,0)
elif ch & 0xFF == ord('g'):
color = (0,255,0)
elif ch & 0xFF == ord('r'):
color = (0,0,255)
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment