简体   繁体   English

如何在 python 中检测 png 中某些像素的 rgb 值?

[英]How do you detect the rgb value of some pixels from a png, in python?

My code takes a screenshot, (should) test if any of the pixels in the screenshot are a general rgb value (in my example they are in the range of: r_min, r_max and g_min, g_max. Also, the end_time is there to make sure that there is enough time to scan all the pixels, since it will then screenshot another photo, and check for the pixels in the other one, until it finds the specific pixels, after that, it changes the value of variable (fish_found) from 0 to 1, and gets through the if statement.我的代码截取了屏幕截图,(应该)测试屏幕截图中的任何像素是否是通用 rgb 值(在我的示例中,它们的范围为:r_min、r_max 和 g_min、g_max。此外,还有 end_time确保有足够的时间扫描所有像素,因为它会截取另一张照片,并检查另一张照片中的像素,直到找到特定像素,然后将变量 (fish_found) 的值从0 到 1,并通过 if 语句。

This is the code that I came up with:这是我想出的代码:

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

Okay, update for anyone needing it: the whole code was a mess, and I found a better way to find out if the pixels match:好的,为任何需要它的人更新:整个代码一团糟,我找到了一种更好的方法来确定像素是否匹配:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Python上的Google App Engine中,如何检测JPEG是否使用CMYK色彩空间以及如何将其转换为RGB? - On Google App Engine in Python how do you detect if a JPEG uses CMYK color space and how do you convert it to RGB? Python枕头改变像素的RGB值? - Python Pillow Changing RGB Value Of Pixels? 如何在 python 中裁剪 PNG 图像? - How do you crop a PNG image in python? 如何在RGB图像中的阴影像素中消除阴影的影响 - How do I remove effects of shadow in shadowed pixels in RGB images Python 如何从堆叠的 RGB 中以光栅形式写入 PNG? - How to write PNG in rasterio from a stacked RGB? 你如何比较像素? - How do you compare pixels? Python比较图像中哪个RGB值具有最多像素 - Python comparing which RGB value has the most pixels in an image 如何将某个 RGB 值分配给 Python 中一些其他 RGB 值的“最近”值 - how to assign a certain RGB Value to the 'nearest' of some other RGB-Values in Python 如何从 RGB 图像中制作像素 arrays 而不会丢失 python 中的空间信息? - How to make pixels arrays from RGB image without losing its spatial information in python? 如何修复 python `dlib` 错误:“在平面命名空间 '_png_do_expand_palette_rgb8_neon' 中找不到符号”? - How to fix python `dlib` error: "symbol not found in flat namespace '_png_do_expand_palette_rgb8_neon'"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM