forked from maksimKorzh/auto-draw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw-matrix-random.py
66 lines (51 loc) · 1.63 KB
/
draw-matrix-random.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
##########################################
#
# Simple script to automate the process
# of drawing using Python
#
# by
#
# Code Monkey King
#
##########################################
# packages
import cv2
import pyautogui as pg
import time
import random
# open source image
originalImage = cv2.imread('image.png')
# convert image to grayscale
grayImage = cv2.cvtColor(originalImage, cv2.COLOR_BGR2GRAY)
# convert image to black and white
(thresh, blackAndWhiteImage) = cv2.threshold(grayImage, 127, 255, cv2.THRESH_BINARY)
# uncomment below lines to preview image
cv2.imshow('Black & white image', blackAndWhiteImage)
cv2.waitKey(0)
cv2.destroyAllWindows()
# global coordinates of where to start drawing (change if needed!)
x_start = 250
y_start = 150
# wait before user clicks on window to within
time.sleep(5)
# put mouse mointer into initial position
pg.moveTo(x_start, y_start)
# get row indexes
random_rows = list(range(len(blackAndWhiteImage)))
# comment two lines below if you want a "printer" like behavior
random_rows = random.sample(range(len(blackAndWhiteImage)), len(blackAndWhiteImage))
random.shuffle(random_rows)
# loop over pixel rows
for y in random_rows:
# randomize cols traversal order
row = blackAndWhiteImage[y]
random_cols = list(range(len(row)))
#random.shuffle(random_cols)
# loop over pixel cols
for x in random_cols:
if row[x] == 0:
# draw pixel!
pg.click(x_start + x, y_start + y, _pause=False)
print('Drawing at:', x_start + x, y_start + y)
# animation speed
time.sleep(0.008)