【问题标题】:Iterating over an image in python在python中迭代图像
【发布时间】:2016-10-17 10:39:26
【问题描述】:

我想在 python 中迭代一个图像。 到目前为止,这是我的代码:

def imageIteration(greyValueImage):
    for (x,y), pixel in np.ndenumerate(greyValueImage):
        vals = greyValueImage[x, y]
        print(vals)

这里的问题是我得到以下异常:

for (x,y), pixel in np.ndenumerate(greyValueImage):
ValueError: too many values to unpack

现在我的问题是解决这个问题的最快方法是什么? 我真的需要将图像分成几个和平,但是采取这一步如何在不尝试的情况下获得必要循环的计数?

感谢您的意见

附: im = Image.open(args["image"]) im_grey = im.convert('LA') # 转换为灰度

【问题讨论】:

    标签: python image loops for-loop iteration


    【解决方案1】:

    你不能那样拆包。做吧

    def imageIteration(greyValueImage):
        for index, pixel in np.ndenumerate(greyValueImage):
            x, y, _ = index
            vals = greyValueImage[x, y]
            print(vals)
    

    因为ndenumerate 返回 2 的 2 值列表和数字。 http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndenumerate.html

    【讨论】:

    • 异常:x, y = index ValueError: 太多值无法解压
    • @JürgenK。你能做 print(index) 看看这个变量到底是什么吗?
    • @Sardorbeck。 x, y = index ValueError: 要解包的值太多
    • @JürgenK。只需删除x, y = index vals = greyValueImage[x, y] print(vals) 并执行print(index)
    • 那么 vals 看起来像这样 (0,0,1), (0,1,1), (0,2,1)。但我仍然只需要迭代前两个值
    【解决方案2】:

    对于多张图片,使用:

    for i in range (0, n):
    

    【讨论】:

      猜你喜欢
      • 2013-11-16
      • 1970-01-01
      • 2018-09-29
      • 2018-01-03
      • 1970-01-01
      • 1970-01-01
      • 2021-09-27
      • 2013-01-12
      • 1970-01-01
      相关资源
      最近更新 更多