-
Notifications
You must be signed in to change notification settings - Fork 2
/
targeter.py
117 lines (89 loc) · 3.28 KB
/
targeter.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""
Randomly point a target in a picture.
Usage:
targeter.py PICTURE_FILE
Press enter to add a new target, backspace to remove the last target from the picture, and escape
to quit. You can also press 'r' to do both, the removing of the last target and the adding of a
new one (typical use: "last target wasn't good, re-roll!")
Also, you can use the zoom controls of the picture to zoom in into the targets, and keys will keep
working as intended. Try it out! :)
"""
import random
import sys
from pathlib import Path
from docopt import docopt
from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle
from PIL import Image
TARGET_FACTOR = 0.09
class Status:
def __init__(self):
self.targets = []
self.keep_targetting = True
def targets_loop(picture):
"""
Loop where the user interacts with the picture by adding and removing targets.
"""
status = Status()
max_width, max_height = picture.size
target_size = min((max_width * TARGET_FACTOR, max_height * TARGET_FACTOR))
def add_target():
new_target = random.randint(0, max_width), random.randint(0, max_height)
status.targets.append(new_target)
print('New target:', new_target)
def remove_target():
if status.targets:
print('Remove last target')
status.targets.pop()
else:
print('No targets to remove')
def draw_target(position, color):
x, y = position
vertical_line = Rectangle((x, y - target_size / 2), 0, target_size,
linewidth=3, edgecolor=color, facecolor='none')
horizontal_line = Rectangle((x - target_size / 2, y), target_size, 0,
linewidth=3, edgecolor=color, facecolor='none')
selection = fig.add_subplot(111)
selection.add_patch(vertical_line)
selection.add_patch(horizontal_line)
def on_key(event):
if event.key == 'escape':
# stop targetting
print('Have a nice day :)')
status.keep_targetting = False
plt.close('all')
elif event.key == 'enter':
add_target()
plt.close('all')
elif event.key == 'backspace':
remove_target()
plt.close('all')
elif event.key == 'r':
remove_target()
add_target()
plt.close('all')
while status.keep_targetting:
ax = plt.imshow(picture)
fig = ax.get_figure()
fig.canvas.set_window_title("Random targets!!")
fig.canvas.mpl_connect('key_press_event', on_key)
fig.tight_layout()
mng = plt.get_current_fig_manager()
mng.resize(*mng.window.maxsize())
plt.axis('off')
last_target = len(status.targets) - 1
for target_number, target in enumerate(status.targets):
if target_number == last_target:
color = 'r'
else:
color = 'b'
draw_target(target, color)
plt.show(block=True)
if __name__ == '__main__':
opts = docopt(__doc__)
picture_path = Path(opts['PICTURE_FILE'])
if not picture_path.exists():
print("No picture found in the specified path")
sys.exit(1)
picture = Image.open(picture_path)
targets_loop(picture)