亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用python點擊圖像的第n個像素(不是你想象的那樣)

如何使用python點擊圖像的第n個像素(不是你想象的那樣)

梵蒂岡之花 2023-08-22 16:52:48
我有一些代碼來確定屏幕上每個黑色像素的位置: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)


查看完整回答
反對 回復 2023-08-22
?
動漫人物

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))


查看完整回答
反對 回復 2023-08-22
  • 2 回答
  • 0 關注
  • 209 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號