Skip to content

Instantly share code, notes, and snippets.

@omaraflak
Created August 22, 2023 15:53
Show Gist options
  • Save omaraflak/0db031e7336466689a8c7de525f802ef to your computer and use it in GitHub Desktop.
Save omaraflak/0db031e7336466689a8c7de525f802ef to your computer and use it in GitHub Desktop.
cv2 canvas
import cv2
import numpy as np
size = 28
drawing = False
win_name = "Draw digit"
def draw(event, x, y, flags, *param):
global drawing
if event == cv2.EVENT_LBUTTONDOWN:
drawing = not drawing
elif event == cv2.EVENT_MOUSEMOVE and drawing:
img[y, x] = 255
cv2.imshow(win_name, img)
img = np.zeros((size, size)).astype(np.uint8)
cv2.namedWindow(win_name)
cv2.setMouseCallback(win_name, draw)
while(1):
cv2.imshow(win_name, img)
k = cv2.waitKey(0)
if k == 27:
break
if k == ord('c'):
img.fill(0)
if k == ord(' '):
print('ok')
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment