如何解决你如何在python中检测png中某些像素的rgb值?
我的代码截取了屏幕截图,(应该)测试屏幕截图中的任何像素是否是一般的 rgb 值(在我的示例中,它们的范围是:r_min、r_max 和 g_min、g_max。此外,end_time 是在那里确保有足够的时间扫描所有像素,因为它随后会截取另一张照片,并检查另一张照片中的像素,直到找到特定像素,然后更改变量( fish_found) 从 0 到 1,并通过 if 语句。
这是我想出的代码:
fish_found = 0
im = screenshot('check.png')
def find_fish(image_name):
r_min = 140
r_max = 190
g_min = 85
g_max = 185
fish_found = 0
img = Image.open(image_name)
rgb = img.convert('RGB')
for x in range(img.size[0]):
for y in range(img.size[1]):
r,g,b = rgb.getpixel((x,y))
if r >= r_min and r <= r_max and g >= g_min and g <= g_max:
fish_found = fish_found + 1
while True:
im = screenshot('check.png',region=(990,415,20,20))
end_time = datetime.Now() + timedelta(seconds=0.5)
while datetime.Now() < end_time:
find_fish('check.png')
print(fish_found)
if fish_found > 0:
print("You found a fish")
解决方法
好的,为任何需要它的人更新:整个代码一团糟,我找到了一个更好的方法来确定像素是否匹配:
def function():
pic = screenshot('screen.png',region=(985,420,30,30))
width,height = pic.size
for x in range(0,width,2):
#modify the last digit to take greater steps,if you don't want to check every
#pixel
for y in range(0,height,2):
#same thing,change the 2 to any value you want
r,g,b = pic.getpixel((x,y))
#this gets the actual rgb value of the pixel
if r in range(250,256) and g in range(195,215) and b in range(28,38):
#put the range of values of r g b up here ^
#down here you place your code (after you find out if they match,what do
#you do should be right here.
#after all of that,just call your function when you want to.
function()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。