【问题标题】:Getting pixels from an image using PIL使用 PIL 从图像中获取像素
【发布时间】:2019-01-21 12:05:48
【问题描述】:

我想编写一个脚本来读取 bmp 文件,然后记录 x-y 轴上的像素,其中图像的颜色不是白色。然后将这些数据保存到 .txt 文件中。

我已经开始使用 Pillow 库,但还没有找到解决这个问题的方法。我用 Pillow 打开了 bmp 文件并尝试使用 Image 中的模块,但我找不到如何使它工作。

提前致谢!

【问题讨论】:

  • 展示您自己的努力和尝试解决问题的代码(如问题中格式正确的文本)。

标签: python python-imaging-library bmp


【解决方案1】:

您可以使用 PIL 中的Image.getpixel() 读取像素。

下面的代码将以二维列表的形式为您提供所有非白色像素。

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)

【讨论】:

    【解决方案2】:

    编辑: 刚刚意识到,问题是要找到与白色不同的所有像素的索引...
    对于这种情况,只需将布尔数组的计算替换为补码函数:

    white = (img[:, :, :3] != 255).any(2)
    

    只需在np.where 中的布尔数组前面添加一个~

    coords = np.array(np.where(~white))[::-1].T
    

    测试结果将因此相反


    这里我假设 “使用 PIL” 不是一个严格的要求,而只是你尝试如何开始的标志。

    也就是说,您可以使用来自imageioimread 执行以下操作:

    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 返回一个与img 相同形状的布尔数组,表示单个 RGB 值的组件匹配(与 A 无关。如果 A 也应该是 255,则省略完整的索引括号)。.all(2) 通过仅将那些索引设置为True 将其缩减为所需的形状(nr, nc) 数组,其中完整的RGBA 数组匹配。

    np.where(white) 返回行索引和列索引的元组。
    np.array() 将其转换为 numpy 数组,以便对其应用 numpy 索引和转换。
    由于row/columnx/y 命名法的相反顺序,[::-1] 颠倒了两个索引数组的顺序。 .T 的转换不是输出两个包含 n 个索引的数组(xy),而是输出 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')
    

    【讨论】:

      猜你喜欢
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-05
      • 1970-01-01
      • 1970-01-01
      • 2013-07-21
      相关资源
      最近更新 更多