为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用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 关注
  • 123 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信