np数组,包含图像中的(R,G,B,X,Y)个像素



我目前正在尝试获取一个像素列表,其中包含它们的rgb以及每个像素的x和y值。我目前可以得到所有像素的平面列表。

import Image
im = Image.open('Lenna.png')
pixels = np.array(im.getdata())

这将为您提供一个RGB数据的平面列表,看起来像:

[(226137125(,(226137125],(223137133(,(223136128(,(226138120(,(226129116(,(228138123(,(227134124(,(227140127(,(225136119(,(228135126(,(225 134 121(,。

然而,这不会给我任何关于每个像素的x和y坐标的信息。我能得到什么建议吗?

如果你想过滤像素,你可以检查评论中提到的问题的答案

如果您需要所有像素的坐标,以下代码可能会有所帮助:

from PIL import Image
import numpy as np

img = Image.open('/your_pic.png')
width, height = img.size
x, y = np.meshgrid(range(width), range(height))
coordinate = np.concatenate([img.getdata(), x.reshape(-1, 1), y.reshape(-1, 1)], axis=1)

然后你会得到一个带有[R,G,B,Alpha,X,Y]的ndarray。

array([[  0,   0,   0, 255,   0,   0],
       [  0,   0,   0, 255,   1,   0],
       [  0,   0,   0, 255,   2,   0],
       ...,
       [  0,   0,   0, 255, 663, 665],
       [  0,   0,   0, 255, 664, 665],
       [  0,   0,   0, 255, 665, 665]])

相关内容

最新更新