python - 使用 PIL 从图像中获取像素

标签 python python-imaging-library bmp

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

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

提前致谢!

最佳答案

您可以使用 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)

关于python - 使用 PIL 从图像中获取像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54289626/

相关文章:

python - 从十六进制数据转换回纬度 GREENTEL

python - 在Python 3中为Windows用户创建程序,无法使用PIL?

python - Django/PIL 错误 - 呈现 : The _imagingft C module is not installed 时捕获异常

delphi - 在 Delphi 中裁剪并对齐插入的 BMP

image - 是否有一种算法可以压缩相差不大的多个图像?

python - 为 PySpark 捆绑 Python3 包导致缺少导入

python - 如何从我的 setup.py 文件中使用 Cython 进行外部模块编译?

python - 使用 python、HTTP/1.1 和自定义用户代理发布表单数据

python - "decoder jpeg not available"在 AWS Elastic Beanstalk 上使用 Pillow

C# 控制台应用程序 - 如何使用 GDI+ 在 BMP/JPG 文件中绘制?