我有一些代碼來確定屏幕上每個黑色像素的位置:last_pixel = 0time.sleep(0.01)ss = pyautogui.screenshot()ss.save(r"Screenshots\ss.png")image = Image.open(r"Screenshots\ss.png", "r")pixels = list(image.getdata())for n, pixel in enumerate(pixels): if pixel == (0, 0, 0): print(pixel, n) last_pixel = n但是,這會返回,例如“(0, 0, 0) 2048576”,并且要單擊屏幕上的特定點,至少使用 pynput/pyautogui,您需要 x, y 之類的東西,我怎樣才能可以單擊圖像(屏幕截圖)的一個像素,只需輸入一個數字,例如:它是第 2048576 個像素,單擊它。
2 回答

慕姐4208626
TA貢獻1852條經驗 獲得超7個贊
如果您知道圖像的大?。▽挾?x 高度),則將像素數轉換為 [x, y] 坐標是一個簡單的數學問題。
img_width = 1920
img_height = 1080
pixel_number = 2000
pixel_row, pixel_col = divmod(pixel_number, img_width)
我不確定像素是否按行優先順序或列優先順序存儲。如果它們按列優先順序存儲,您需要做的就是:
pixel_col, pixel_row = divmod(pixel_number, img_height)

動漫人物
TA貢獻1815條經驗 獲得超10個贊
將像素列表重新調整為圖像的尺寸,并獲取該位置的像素索引。例如
import numpy as np
# 150 pixel long array
a = np.array(range(150))
# If your images was 15 pixels wide by 10 tall find the coordinates of pixel 108
np.where(a.reshape((10,15))==108)
輸出
(array([7], dtype=int64), array([3], dtype=int64))
添加回答
舉報
0/150
提交
取消