Debian Codegraph @

eg/extras/FindControlVisually.pl

raw
1 #!/usr/bin/perl
2 #-----------------------------------------------------------------------------#
3 # X11::GUITest ($Id: FindControlVisually.pl 253 2026-08-19 22:37:51Z ctrondlp $)
4 # Notes: Example of script to locate a widget/control visually using an
5 # image baseline. Uses ImageMagick's 'compare' and 'convert'
6 # command-line tools (part of the imagemagick package) rather than
7 # a dedicated CPAN subimage-search module -- the couple that exist
8 # (Image::SubImageFind, Image::Match) have both been unmaintained
9 # for over a decade, while ImageMagick itself is actively developed.
10 #-----------------------------------------------------------------------------#
11
12 ## Pragmas/Directives/Diagnostics ##
13 use strict;
14 use warnings;
15
16 ## Imports (use [MODULE] qw/[IMPORTLIST]/;) ##
17 use File::Temp qw/:POSIX/;
18 use X11::GUITest qw/
19 MoveMouseAbs
20 ClickMouseButton
21 :CONST
22 /;
23
24 ## Constants (sub [CONSTANT]() { [VALUE]; }) ##
25 # Normalized RMSE distortion (0 = perfect match, 1 = no similarity at all)
26 # at or below which a match is accepted. Tune to taste -- a baseline
27 # image with more size/detail and less repetitive background allows a
28 # lower/stricter threshold.
29 use constant MATCH_THRESHOLD => 0.15;
30
31 ## Variables (my [SIGIL][VARIABLE] = [INITIALVALUE];) ##
32
33 ## Core ##
34 print "$0 : Script Start\n";
35
36 print "Locating the control...\n";
37 # Find the control on-screen using baseline image to compare to
38 my ($x, $y) = FindScreenObject('/test/but_superscript.png');
39 if ($x >= 0 && $y >= 0) {
40 print "Found at $x X $y\n";
41 #MoveMouseAbs $x, $y;
42 #ClickMouseButton M_LEFT;
43 } else {
44 print "Not found\n";
45 }
46
47 print "$0 : Script End (Success)\n";
48
49 ## Subroutines ##
50 sub FindScreenObject {
51 my $baseline = shift; # baseline sub-image/clip to find on screen
52 my $maxwait = shift || 30; # seconds to wait for discovery
53
54 if (!-e $baseline) {
55 die("Baseline $baseline image does not exist");
56 }
57
58 for (my $i = 1; $i <= $maxwait; $i++) {
59 # Results may vary, depending on baseline quality and detail, etc.
60 # In general, a larger (50x50 pixels) baseline with good detail will fair good.
61 my $scrfile = GetScreenshot();
62 my ($x, $y) = FindSubImage($scrfile, $baseline);
63 unlink $scrfile;
64 if ($x >= 0 && $y >= 0) {
65 return ($x, $y);
66 }
67 sleep(1);
68 }
69 return (-1, -1);
70 }
71
72 # Uses ImageMagick's 'compare -subimage-search' to locate $baseline within
73 # $scrfile. Returns the top-left (x, y) of the best match, or (-1, -1) if
74 # no match at or under MATCH_THRESHOLD was found.
75 sub FindSubImage {
76 my ($scrfile, $baseline) = @_;
77
78 my $diff = tmpnam();
79 my $output = `compare -metric RMSE -subimage-search '$scrfile' '$baseline' '$diff' 2>&1`;
80 unlink $diff;
81
82 # compare's stderr looks like: "7842.99 (0.119679) @ 351,127"
83 if ($output =~ /\(([\d.]+)\)\s*\@\s*(\d+)\s*,\s*(\d+)/) {
84 my ($distortion, $mx, $my) = ($1, $2, $3);
85 return ($distortion <= MATCH_THRESHOLD) ? ($mx, $my) : (-1, -1);
86 }
87 return (-1, -1);
88 }
89
90 sub GetScreenshot {
91 my $file = tmpnam();
92 system("xwd -root | convert xwd:- '$file'");
93 return $file;
94 }