Thư viện python đơn giản hữu ích để tự động hóa các tác vụ bằng hình ảnh. Nó có thể chạy trên các ứng dụng nền Windows
Nó sử dụng OpenCV và PyAutoGui. Được tạo bằng Python 3. 7
ví dụ đơn giản
import cv2 from gui_automation import GuiAuto image_path = "win10key.png" ga = GuiAuto[] if ga.detect[cv2.imread[image_path], 0.8]: ga.click[]
Nó tìm kiếm hình ảnh chính của windows 10 trên màn hình với độ tương đồng từ 80% trở lên. Nếu nó được tìm thấy, nó sẽ được nhấp [nó sẽ mở menu bắt đầu của windows 10]
Lớp cốt lõi là GuiAutoKết thúc quá trình phát hiện và kiểm soát GUI. Theo mặc định, nó sẽ sử dụng tính năng phát hiện hình ảnh thông thường và tự động hóa ứng dụng tiền cảnh
Thông số
detector: detector instance. Default is TMDetector[] handler: handler instance. Default is ForegroundHandler[]
phương pháp
detect[tpl, img=None]: returns Spot instance if it finds the tpl in the image. Internally, it keeps the last spot found. move[coords=None]: same as detect but it moves the cursor to the center of the found image withing the screen. click[coords=None]: Clicks the left buttons the quantity specified in @param click[default 1] in the center of the found image. hold[coords=None]: Same as click but instead of clicking X times, it holds the click @param time seconds. drag[start_coords, end_coords]: Drags from one point to another using start and end coordinates. drag_within[start_x_fraction, start_y_fraction, end_x_fraction, end_y_fraction]: Drags from one point to another inside the bounding box of the image found. For move, click, and hold methods if no coords are given it performs the action on the last spot found.
phương thức drag_within
Drags the mouse from one point to another using the tpl image width and height. All params are fractions in the following string format: 'number/number'. Eg: ga.drag_within['3/4', '0/1', '7/8', '4/5'] 3/4 of the width and 0/1 of the height for START __o___o_ 7/8 of the width for END | S | S = start | \\ | E = end | \\ | \\ = the mouse drag path | E o 4/5 of the height for END |________|
Tìm kiếm một hình ảnh bên trong một hình ảnh khác bằng cách sử dụng đối sánh mẫu từ OpenCV. Các lớp có tham số mặc định
TMDetector[method=SQDIFF, thresh=False]:
Applies the normal detection.
MultiscaledTMDetector[method=SQDIFF, thresh=False, reduce_sc=0.2, magnify_sc=2.0, cant_sc=40]:
Applies detection multiples times while resizing the image. Parameters specify how image is resized.
Thông số
method: template method to use. Could be SQDIFF, CCOEFF or CCORR. thresh: boolean that specifies if binary threshold filter must be used for detection. reduce_sc: how much the image is reduced. magnify_sc how much the image is enlarged. cant_sc= how many resizing will be applied.
Tương tác với ứng dụng hoặc môi trường sẽ được tự động hóa. Thực hiện nhấp chuột, kéo giữa những người khác;
ForegroundHandler[]:
Normal handler that takes screenshot and simulates mouse action normally.
BackgroundHandlerWin32[app_name, *args]:
Handler that works in not vieawable/background applications. It requires an application/window name, and it's possible to pass as arguments a names hierarchy of the UI elements of the application.
Works only for Windows.
NơiKết thúc tất cả các tính toán vị trí/tọa độ cho hình ảnh được tìm thấy
phương pháp
upper_left_position[] upper_right_position[] bottom_left_position[] bottom_right_position[] center_position[] custom_position[x_multiplier, x_modifier, y_multiplier, y_modifier]
phương pháp custom_position
________số 8
Mô-đun nhỏ giúp tải hình ảnh từ một thư mục. Tải tất cả hình ảnh của một thư mục được cung cấp trong đường dẫn param và gán chúng cho từ điển theo cách này. tên=>tên hình ảnh sẽ là tên tệp không có phần mở rộng. hình ảnh sẽ là mảng gọn gàng với dữ liệu hình ảnh được tải bằng OpenCV. Đường dẫn. đường dẫn tương đối hoặc tuyệt đối nơi hình ảnh được. Phải kết thúc bằng '/'. Nó trả về một từ điển với tên của hình ảnh là khóa và bản thân hình ảnh là giá trị. Trả về Sai nếu có lỗi
from gui_automation import GuiAuto, load_images from time import sleep buttons = load_images["images/buttons/"] ga = GuiAuto[] while not ga.detect[buttons['start'], 0.8].detect[]: ga.click[] sleep[2] if ga.detect[buttons['accept'], 0.8]: print["Found accept button"]
Trong trường hợp này, chúng tôi tải tất cả hình ảnh trong thư mục "hình ảnh/nút/" và đợi cho đến khi tìm thấy nút bắt đầu. Sau đó, nó đợi 2 giây rồi cố gắng tìm nút chấp nhận
Một vi dụ khac detector: detector instance. Default is TMDetector[]
handler: handler instance. Default is ForegroundHandler[]
0Trong trường hợp này, chúng tôi có một hình ảnh win10key tương tự như hình ảnh ban đầu nằm trên màn hình được hiển thị của chúng tôi. Vì vậy, việc sử dụng MultiscaledTMDetector sẽ khắc phục sự cố thay đổi kích thước màn hình nhiều lần của chúng tôi. Nếu nó được phát hiện, nó sẽ nhấp vào dưới cùng bên phải của hình ảnh được tìm thấy ba lần