簡體   English   中英

使用python PIL獲取特定顏色的像素的x,y值

[英]Get x, y values of pixels of certain color with python PIL

我真的很難理解如何使用PIL處理圖像。 我正在嘗試返回與某種顏色匹配的所有像素的x和y坐標。 因此在偽代碼中:

img = ImageGrab.grab(bbox)
pixels = img.getdata()
for i in range(len(pixels)):
    if pixels[i] == (255, 0, 0, 255) # red for example:
        coords.append(pixels[i].x)
        coords.append(pixels[i].y)

我只是不知道在附加x和y的最后一點。 有這個功能嗎?

謝謝!

像這樣:

from PIL import ImageGrab

img = ImageGrab.grab()
pixels = img.load()
width, height = img.size
coords = []
for x in range(width):
    for y in range(height):
        if pixels[x, y] == (255, 0, 0):
            coords.append((x, y))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM