我的图片A的尺寸为(512、512、3)。
我想找到所有== [255,255,255]的像素。
鉴于像素,我想在另一幅图像B中为这些坐标着色。
我在做什么错了?
indices = np.where(imgA!= [255,255,255])
imgB[indices] = [0,0,0]
答案 0 :(得分:0)
此模板应使您走上正确的道路:
from PIL import image
picture = Image.open(path_to_picture)
width, height = picture.size
for x in range(width):
for y in range(height):
current_color = picture.getpixel( (x,y) )
if current_color[0:3]!=(255,255,255):
picture.putpixel( (x,y), (***, ***,***) + (current_color[-1],))
picture.save(path_to_new_picture)
请注意,getpixel()
将返回一个元组,其中包含给定像素的RGBA值。在此示例中,我假设您保留了alpha值,并且只是修改了当前像素的RGB值。
答案 1 :(得分:0)
您需要遍历图像中的每个像素。
... imgA!= [255,255,255]
将始终返回true,因为您正在将(512,512,3)nd.array与(3,)nd.array进行比较
即使您的图像不是由numpy矩阵构建的,这一点仍然适用。如果遇到性能问题,请使用 cython 加快循环速度。