使用 PIL 从图像中获取像素
Getting pixels from an image using PIL
我想编写一个脚本来读取 bmp 文件,然后记录 x-y 轴上的像素,其中图像是白色以外的颜色。然后将此数据保存到 .txt 文件中。
我已经开始使用 Pillow 库,但还没有找到解决这个问题的方法。我用 Pillow 打开 bmp 文件并尝试使用 Image 中的模块,但我找不到如何使它工作。
提前致谢!
您可以使用 Image.getpixel()
从 PIL 读取像素。
下面的代码将以二维列表的形式为您提供所有非白色像素。
from PIL import Image
im = Image.open('x.bmp')
imageSizeW, imageSizeH = im.size
nonWhitePixels = []
for i in range(1, imageSizeW):
for j in range(1, imageSizeH):
pixVal = im.getpixel((i, j))
if pixVal != (255, 255, 255):
nonWhitePixels.append([i, j])
print(nonWhitePixels)
编辑:
刚刚意识到,问题是关于找到与白色 不同 的所有像素的索引...
对于这种情况,只需将布尔数组的计算替换为其补码函数即可:
white = (img[:, :, :3] != 255).any(2)
或
只需在np.where
中的布尔数组前加一个~
即可:
coords = np.array(np.where(~white))[::-1].T
测试结果会因此相反
这里我假设 "using PIL" 不是严格的要求,而只是您尝试开始的标志。
也就是说,您可以使用 imageio
中的 imread
执行以下操作:
import numpy as np
from imageio import imread
img = imread(filename)
white = (img[:, :, :3] == 255).all(2)
coords = np.array(np.where(white))[::-1].T
写入 ascii 文件可以像
一样完成
with open('xycoordinates.txt', 'w') as f:
for c in coords:
f.write(f'{c[0]},{c[1]}\n')
解释
img
是一个 3D 数组,形状为 (nr, nc, c)
,即行数、列数和 RGBA 值。
img[:, :, :3] == 255
returns 一个与 img
形状相同的布尔数组,表示单个 RGB 值的组件匹配(不考虑 A。如果 A 也应该是 255,则只留下完整的索引括号)。
.all(2)
通过仅将那些索引设置为 True
将其减少到所需的形状数组 (nr, nc)
,其中完整的 RGBA 数组匹配。
np.where(white)
returns 行和列索引的元组。
np.array()
将其转换为 numpy 数组,以便对其应用 numpy 索引和转换。
由于 row/column
是 x/y
命名法的相反顺序,因此 [::-1]
颠倒了两个索引数组的顺序。
.T
的转换不是输出 n 个索引的两个数组(x
和 y
),而是输出 n 个 (x, y)
索引的数组。
例子
img = imread(samplefile)
plt.imshow(img)
white = (img == [255, 255, 255, 255]).all(2)
array([[False, False, True, False, False, False],
[False, False, True, True, False, False],
[False, True, False, False, False, False],
[ True, True, False, False, False, False],
[False, False, False, False, False, True]])
coords = np.array(np.where(white))[::-1].T
array([[2, 0],
[2, 1],
[3, 1],
[1, 2],
[0, 3],
[1, 3],
[5, 4]], dtype=int64)
plt.plot(*coords.T, 'ro')
我想编写一个脚本来读取 bmp 文件,然后记录 x-y 轴上的像素,其中图像是白色以外的颜色。然后将此数据保存到 .txt 文件中。
我已经开始使用 Pillow 库,但还没有找到解决这个问题的方法。我用 Pillow 打开 bmp 文件并尝试使用 Image 中的模块,但我找不到如何使它工作。
提前致谢!
您可以使用 Image.getpixel()
从 PIL 读取像素。
下面的代码将以二维列表的形式为您提供所有非白色像素。
from PIL import Image
im = Image.open('x.bmp')
imageSizeW, imageSizeH = im.size
nonWhitePixels = []
for i in range(1, imageSizeW):
for j in range(1, imageSizeH):
pixVal = im.getpixel((i, j))
if pixVal != (255, 255, 255):
nonWhitePixels.append([i, j])
print(nonWhitePixels)
编辑:
刚刚意识到,问题是关于找到与白色 不同 的所有像素的索引...
对于这种情况,只需将布尔数组的计算替换为其补码函数即可:
white = (img[:, :, :3] != 255).any(2)
或
只需在np.where
中的布尔数组前加一个~
即可:
coords = np.array(np.where(~white))[::-1].T
测试结果会因此相反
这里我假设 "using PIL" 不是严格的要求,而只是您尝试开始的标志。
也就是说,您可以使用 imageio
中的 imread
执行以下操作:
import numpy as np
from imageio import imread
img = imread(filename)
white = (img[:, :, :3] == 255).all(2)
coords = np.array(np.where(white))[::-1].T
写入 ascii 文件可以像
一样完成with open('xycoordinates.txt', 'w') as f:
for c in coords:
f.write(f'{c[0]},{c[1]}\n')
解释
img
是一个 3D 数组,形状为 (nr, nc, c)
,即行数、列数和 RGBA 值。
img[:, :, :3] == 255
returns 一个与 img
形状相同的布尔数组,表示单个 RGB 值的组件匹配(不考虑 A。如果 A 也应该是 255,则只留下完整的索引括号)。
.all(2)
通过仅将那些索引设置为 True
将其减少到所需的形状数组 (nr, nc)
,其中完整的 RGBA 数组匹配。
np.where(white)
returns 行和列索引的元组。
np.array()
将其转换为 numpy 数组,以便对其应用 numpy 索引和转换。
由于 row/column
是 x/y
命名法的相反顺序,因此 [::-1]
颠倒了两个索引数组的顺序。
.T
的转换不是输出 n 个索引的两个数组(x
和 y
),而是输出 n 个 (x, y)
索引的数组。
例子
img = imread(samplefile)
plt.imshow(img)
white = (img == [255, 255, 255, 255]).all(2)
array([[False, False, True, False, False, False],
[False, False, True, True, False, False],
[False, True, False, False, False, False],
[ True, True, False, False, False, False],
[False, False, False, False, False, True]])
coords = np.array(np.where(white))[::-1].T
array([[2, 0],
[2, 1],
[3, 1],
[1, 2],
[0, 3],
[1, 3],
[5, 4]], dtype=int64)
plt.plot(*coords.T, 'ro')