【问题标题】:How to analyse an image in python pixel by pixel如何逐像素分析python中的图像
【发布时间】:2018-04-09 08:07:35
【问题描述】:

我编写的代码应该允许我遍历每列的 y 值并返回每个像素的 RBG 值。我试图在我的图像中找到所有纯白色像素。但是由于某种原因,当找到第一列中的最终值时,出现错误:“IndexError:图像索引超出范围”。我怎样才能进入下一列?

我的代码如下所示:

from PIL import Image

pix = newimage2.load()
print(newimage2.size)
print(" ")

whitevalues = 0
x = 0
while x <= newimage2.width:
    y = 0
    while y <= newimage2.height:
        print(pix[x,y])
        if pix[x,y] == (255,255,255):
            whitevalues = whitevalues + 1
        y = y+1
    x = x+1
print(whitevalues)

【问题讨论】:

    标签: python python-imaging-library


    【解决方案1】:

    Python 是zero-indexed,即如果你有一个list,例如:

    l = [1, 4, 7, 3, 6]
    

    如果你想通过iterate 使用while loopfor-loop 会更好,但没关系),那么你将不得不loop while@987654329 @ 小于listlength - 所以index 实际上从来不是listlength,只是之前的1。 p>

    iterating 上面 list 的代码如下所示:

    i = 0
    while i < len(l):
       print(l[i])
       i += 1
    

    这会给你output

    1
    4
    7
    3
    6
    

    同样的逻辑也适用于你的image——毕竟它本质上只是一个2-dimensionallist

    这意味着您需要将代码中的 less than or equal (&lt;=) 比较器更正为 less thans (&lt;)。然后你的代码应该按照你想要的方式运行。

    所以这将是更正的代码:

    from PIL import Image
    pix = newimage2.load()
    print(newimage2.size)
    print(" ")
    whitevalues = 0
    x = 0
    while x < newimage2.width:
        y = 0
        while y < newimage2.height:
            print(pix[x,y])
            if pix[x,y] == (255,255,255):
                whitevalues += 1
            y += 1
        x += 1
    
    print(whitevalues)
    

    然而,正如我在开头提到的,for-loop 更适合这个应用程序,因为它需要更少的行并且更 Pythonic。所以这里是for-loop 的代码,您可能会发现它很有用:

    from PIL import Image
    pix = newimage2.load()
    print(newimage2.size)
    print(" ")
    whitevalues = 0
    for row in newimage2
        for col in row:
            print(col)
            if col == (255,255,255):
                whitevalues += 1
    
    print(whitevalues)
    

    或者,如果你想变得非常 Pythonic,你可以在 list-comprehension 中做到这一点:

    whitevalues = sum([1 for r in pix for c in r if c == 1])
    

    【讨论】:

      【解决方案2】:

      您只需在两个 while 循环中将 '

      主要原因是索引从 0 开始。因此,如果您查看图像大小,它将是 (100,100),但如果您尝试访问像素 pix[100,100],则它不存在。

      但是 pix[99,99] 存在并且对应 pix[100,100]。

      干杯,

      【讨论】:

        【解决方案3】:

        使用零索引,最后一个索引比大小小一。所以您需要将您的&lt;= 更改为&lt;。此外,这个问题应该有一个index 标签。

        您可以通过多种方式使用内置函数来执行此任务。有关示例,请参见此问题。 How to count the occurrence of certain item in an ndarray in Python? 。这些解决方案很可能会明显更快。

        【讨论】:

          【解决方案4】:

          请尝试以下代码,而不是 while 循环:

          [width, height] = newimage2.size 
          for x in range(width):
              for y in range(height):
                  cpixel = pixels[x, y]
                  if(cpixel ==(255,255,255):
                      whitevalues = whitevalues + 1
          

          这将确保索引在范围内。

          【讨论】:

            【解决方案5】:

            其他答案解释了为什么您的代码不起作用,所以这只是为了展示另一种计算白色像素的方法:

            from PIL import Image
            
            image_path = '/path/to/image'
            image = Image.open(image_path)
            count = 0
            
            # image.getdata() returns all the pixels in the image
            for pixel in image.getdata():
                if pixel == (255, 255, 255):
                    count += 1
            
            print(count)
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-12-20
              • 2012-12-31
              • 2016-12-18
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多