我有一个.png文件,我想通过扫描图像来检查其中是否有某个RGB值。例如,假设我有一个图像,希望检查RGB值(255, 0, 0)是否在图像中的某个位置。我如何在Python中做到这一点?谢谢!
发布于 2020-04-01 01:16:16
我建议您使用皮尔-盖特皮恩或PIL-Getdata
from PIL import Image
im = Image.open('whatever.png').convert("RGB")
# get pixels
pixels = [im.getpixel((i, j)) for j in range(im.height) for i in range(im.width)]
# or
pixels = [i for i in im.getdata()]
#check if tuple of pixel value exists in array-pixel
print((255, 0, 0) in pixels) #True if exists, False if it doesn't发布于 2020-04-01 01:39:51
这应该管用..。
import cv2
import numpy as np
img = cv2.imread(r'circle.png')
ind = np.where((img[:, :, 0]==255) & (img[:, :, 1]==0) & (img[:, :, 2]==0))
answer = list(zip(ind[0], ind[1]))
print(answer) # Prints row and column indices in tuples发布于 2020-04-01 00:56:40
您可以使用cv2包加载图像,并使用numpy作为数组搜索图像:
import cv2
import numpy as np
img = cv2.imread('one.png')
pixel = img[801,600]
print (pixel) # pixel value i am searching for
def search_array():
pixel_tile = np.tile(pixel, (*img.shape[:2], 1))
diff = np.sum(np.abs(img - pixel_tile), axis=2)
print("\n".join([f"SUCCESS - {idx}" for idx in np.argwhere(diff == 0)]))
if __name__ == "__main__":
search_array()摘自我的回答,这里。
https://stackoverflow.com/questions/60962082
复制相似问题