我想编写一个脚本来读取 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/