簡體   English   中英

解包像素元組列表

[英]Unpacking list of tuples of pixels

我一直在嘗試解決這個教科書問題,但有點卡住了。

題:

我們提供了一個帶有程序的模塊image

 file2image(filename)

讀取存儲在.png格式文件中的圖像。 導入此過程並調用它,提供包含此格式圖像的文件名作為參數,將返回值分配給變量data

data 的值是一個列表列表,而data[y][x]是像素(x,y)的強度。 像素(0,0)位於圖像的左下角,像素(width-1, height-1)位於右上角。 像素的強度是一個介於 0(黑色)和 255(白色)之間的數字。 使用pts將一組復數x+yi分配給列表pts ,使得像素(x, y)的圖像強度小於 120。

這是提供的相關方法

def file2image(path):
    """ Reads an image into a list of lists of pixel values (tuples with
        three values). This is a color image. """
    (w, h, p, m) = png.Reader(filename = path).asRGBA() # force RGB and alpha
    return [_flat2boxed(r) for r in p]

我真的不確定如何將 3 個值解析為理解,有人猜嗎?

我理解數據結構的方式是:[[(x,y,z)],[(x,y,z)]...等]

所以我的代碼顯然是錯誤的,但我試過了

    data = img.file2image("img01.png")
    data = img.color2gray(data)
    pts = [(x,y,z) for (x,y,z) in data]
    plot(pts)

這是一個非常糟糕的描述文本來描述目標,這是我對列表列表和列表列表列表的理解

>>> [[v for v in range(5)] for _ in range(5)]
[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]
>>> [[[v for v in range(5)] for _ in range(5)] for _ in range(5)]
[[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]], [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]], [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]], [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]], [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]]
>>>

我的for循環中的_丟棄了來自迭代器的變量,因為我不需要存儲它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM