【问题标题】:Python - get white pixels of imagePython - 获取图像的白色像素
【发布时间】:2014-05-02 05:23:31
【问题描述】:

我想从图像文件名转到图像中白色像素的坐标列表。

我知道它涉及 PIL。我曾尝试使用Image.load(),但这无济于事,因为输出不可索引(在for 循环中使用)。

【问题讨论】:

标签: python image numpy python-imaging-library pixel


【解决方案1】:

您可以将图像转储为 numpy 数组并以这种方式操作像素值。

from PIL import Image
import numpy as np
im=Image.open("someimage.png")
pixels=np.asarray(im.getdata())
npixels,bpp=pixels.shape

这将为您提供一个数组,其尺寸取决于您每个像素有多少条带(上面的 bpp)以及行数乘以图像中的列数 - 形状将为您提供结果数组的大小.获得像素值后,应该很容易过滤掉那些值为 255 的像素

要将 numpy 数组转换回图像,请使用:

im=Image.fromarray(pixels)

【讨论】:

  • 遗憾的是,numpy 不是我的选择,但元组索引解决了我的问题
  • @Liam 我不得不使用图像大小并遍历来自im.load() 的数据:pixel = data[x,y]
猜你喜欢
  • 2016-04-30
  • 1970-01-01
  • 2020-07-25
  • 1970-01-01
  • 2015-05-05
  • 1970-01-01
  • 1970-01-01
  • 2019-07-27
  • 1970-01-01
相关资源
最近更新 更多