Skip to content

Commit acb7260

Browse files
melissawmdalthviz
andcommitted
Add pyautogui workflow
Co-authored-by: Daniel Althviz Moré <[email protected]>
1 parent d3b9b68 commit acb7260

File tree

7 files changed

+148
-0
lines changed

7 files changed

+148
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import pyautogui
2+
3+
print('Press Ctrl-C to quit.')
4+
try:
5+
while True:
6+
x, y = pyautogui.position()
7+
positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
8+
print(positionStr, end='')
9+
print('\b' * len(positionStr), end='', flush=True)
10+
except KeyboardInterrupt:
11+
print('\n')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# This script generates the video for the points layer tutorial.
2+
3+
# Before running, make sure to:
4+
# 1. Install napari in development mode from the main branch
5+
# 2. Run `napari --reset`
6+
7+
# To generate the video, install pyautogui and run:
8+
# python webm_add_points.py
9+
10+
from pyautogui import click, alert, locateCenterOnScreen, moveTo, dragTo, screenshot
11+
import numpy as np
12+
from qtpy import QtCore
13+
from skimage import data
14+
from qtpy.QtWidgets import QApplication
15+
import napari
16+
import time
17+
18+
19+
def apply_event(app, event, loc, msg=""):
20+
duration = 0.3
21+
if event == "click_button":
22+
button = locateCenterOnScreen(loc)
23+
click(button, duration=duration)
24+
if event == "click":
25+
click(*loc, duration=duration)
26+
if event == "move":
27+
moveTo(*loc, duration=duration)
28+
if event == "drag":
29+
dragTo(*loc, button="left", duration=duration)
30+
app.processEvents()
31+
print(msg)
32+
app.processEvents()
33+
34+
35+
def run_actions():
36+
# Function that will be triggered from QTimer to run code even when running
37+
# `napari.run`
38+
print("Initial screenshot")
39+
app = QApplication.instance()
40+
time.sleep(1)
41+
print("Done")
42+
43+
# 1. Click add points icon
44+
# Locate coordinates for the `Add points` buttons by using a image of the
45+
# button
46+
apply_event(
47+
app,
48+
"click_button",
49+
"../../images/point-adding-tool.png",
50+
msg="Click add points icon",
51+
)
52+
53+
# 2. Add three new points
54+
apply_event(app, "click", (700, 400), msg="Click add point 1")
55+
apply_event(app, "click", (750, 200), msg="Click add point 2")
56+
apply_event(app, "click", (650, 300), msg="Click add point 3")
57+
58+
# 3. Click select points icon
59+
apply_event(
60+
app,
61+
"click_button",
62+
"../../images/point-selecting-tool.png",
63+
msg="Click select points icon",
64+
)
65+
66+
# 4. Select two points individually
67+
apply_event(app, "click", (750, 200), msg="Click select point 1")
68+
apply_event(app, "click", (650, 300), msg="Click select point 2")
69+
70+
# 5. Drag mouse to select group of points
71+
apply_event(app, "move", (400, 100), msg="Move to selection start")
72+
apply_event(app, "drag", (600, 300), msg="Drag and select")
73+
74+
# 6. Change face color
75+
apply_event(app, "move", (150, 240), msg="Move to face color selection")
76+
apply_event(app, "click", (150, 240), msg="Click face color selection")
77+
apply_event(app, "click", (200, 240), msg="Click face color grid")
78+
apply_event(app, "click_button", "../../images/ok.png", msg="Click OK")
79+
80+
# 7. Change edge color
81+
apply_event(app, "move", (150, 270), msg="Move to edge color selection")
82+
apply_event(app, "click", (150, 270), msg="Click edge color selection")
83+
apply_event(app, "click", (220, 310), msg="Click edge color grid")
84+
apply_event(app, "click_button", "../../images/ok.png", msg="Click OK")
85+
86+
# 8. Select group of points with different colors
87+
apply_event(app, "move", (400, 200), msg="Move to selection start")
88+
apply_event(app, "drag", (600, 400), msg="Drag and select")
89+
90+
# 9. Use slider to increase point size
91+
apply_event(app, "move", (175, 155), msg="Move to point size slider")
92+
apply_event(app, "drag", (210, 155), msg="Drag slider")
93+
94+
# 10. Select a group of points, click symbol dropdown and select cross
95+
apply_event(app, "move", (480, 215), msg="Move to selection start")
96+
apply_event(app, "drag", (680, 345), msg="Drag and select")
97+
apply_event(app, "click", (295, 200), msg="Click symbol dropdown")
98+
apply_event(app, "click", (295, 170), msg="Click cross symbol")
99+
100+
# 11. Use slider to decrease and increase opacity
101+
apply_event(app, "move", (270, 132), msg="Move to opacity slider")
102+
apply_event(app, "drag", (180, 132), msg="Drag slider down")
103+
apply_event(app, "drag", (270, 132), msg="Drag slider up")
104+
105+
# 12. Select point and click the "delete selected points" icon
106+
apply_event(app, "click", (440, 380), msg="Select one point")
107+
apply_event(app, "click_button", "../../images/point-deleting-tool.png", msg="Delete point")
108+
109+
# 13. Click the add points icon
110+
apply_event(app, "click_button", "../../images/point-adding-tool.png", msg="Click add points icon")
111+
112+
# 14. Use the face color dropdown to select a different color
113+
apply_event(app, "click", (150, 240), msg="Click face color selection")
114+
apply_event(app, "click", (310, 180), msg="Click face color grid")
115+
apply_event(app, "click_button", "../../images/ok.png", msg="Click OK")
116+
117+
# 15. Use the slider to increase point size and add new points
118+
apply_event(app, "click", (210, 155), msg="Click point size slider")
119+
apply_event(app, "drag", (170, 155), msg="Drag slider down")
120+
apply_event(app, "click", (500, 500), msg="Click add point")
121+
apply_event(app, "move", (800, 600), msg="Move mouse")
122+
123+
screenshot("fallback.png")
124+
125+
126+
viewer = napari.Viewer()
127+
viewer.window.set_geometry(0, 0, 800, 600)
128+
viewer.add_image(data.astronaut(), rgb=True)
129+
points = np.array([[100, 100], [200, 200], [300, 100]])
130+
points_layer = viewer.add_points(points, size=30)
131+
# Wait a bit so that the user has time to move the app to the foreground
132+
print("Make sure the qt app is in the foreground... waiting 3s to trigger actions")
133+
QtCore.QTimer.singleShot(3000, run_actions)
134+
135+
print("Launched napari")
136+
napari.run()

docs/images/ok.png

952 Bytes
Loading

docs/images/point-adding-tool.png

1.21 KB
Loading

docs/images/point-deleting-tool.png

612 Bytes
Loading

docs/images/point-selecting-tool.png

978 Bytes
Loading

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ napari-sphinx-theme
1313
matplotlib
1414
lxml
1515
imageio-ffmpeg
16+
pyautogui

0 commit comments

Comments
 (0)