Last active
September 24, 2024 10:07
-
-
Save tamnguyenvan/483f4e3f6df1119d2d286a8d8a57a1a0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from Cocoa import NSCursor, NSImage, NSBitmapImageRep, NSPNGFileType | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| from io import BytesIO | |
| import cv2 | |
| def get_cursor_info(): | |
| # Lấy con trỏ hiện tại | |
| cursor = NSCursor.currentSystemCursor() | |
| if cursor: | |
| # Lấy hình ảnh của con trỏ | |
| image = cursor.image() | |
| # Lấy kích thước của con trỏ | |
| size = image.size() | |
| width, height = int(size.width), int(size.height) | |
| # Chuyển đổi NSImage thành NSBitmapImageRep | |
| bitmap_rep = NSBitmapImageRep.imageRepWithData_(image.TIFFRepresentation()) | |
| # Chuyển đổi NSBitmapImageRep thành PNG data | |
| png_data = bitmap_rep.representationUsingType_properties_(NSPNGFileType, None) | |
| # Chuyển đổi PNG data thành numpy array | |
| buffer = BytesIO(png_data) | |
| img_array = plt.imread(buffer, format='png') | |
| cv2.imshow('img', img_array) | |
| cv2.waitKey(10) | |
| return (width, height), img_array | |
| else: | |
| print("Không thể lấy thông tin con trỏ") | |
| return None, None | |
| while True: | |
| get_cursor_info() | |
| time.sleep(0.01) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment