2017-09-25 27 views
0

我的代碼:的Python 3.6:追加了3D名單

Images = [Img1, Img2, Img3, Img4, Img5, Img6, Img7, Img8, Img9] 
i = len(Images) 
tmp_list=[[[0]*3]*ImgHeight]*ImgWidth 

for i in range(i): 
    for source_x in range(ImgWidth): 
     for source_y in range(ImgHeight): 
      tmp = Images[i].getpixel((source_x,source_y)) 
      for tmp1 in range(3): 
       tmp_list[source_x][source_y][tmp1].insert(tmp[tmp1]) 

我要保存的每個像素的顏色通道在一個單獨的列表,從列表中的每個圖像。爲此我有一個3D列表。 我得到了錯誤消息:

AttributeError: 'int' object has no attribute 'insert'

當我使用

print(tmp_list[source_x][source_y][tmp1]) 

輸出:

[43, 23, 76, 24, 97,234, 243,21,23]

+0

請不要這樣做:'tmp_list = [[[0] * 3] * ImgHeight] * ImgWidth'您正在複製參考。 –

+0

好的,你的提示是什麼? 我該如何做得更好? –

+0

我的建議是使用'np.zeros'或'np.empty'。我假設你的圖像是numpy數組。在這種情況下,你應該解釋你想要在更高層次上做什麼,可能還有一些樣例輸入和輸出(比如說)3x3圖像。應該有一個更簡單的方法來解決這個問題。 –

回答

1

我會建議你使用這樣的:

from itertools import product 

ImgHeight = list(range(8)) 
ImgWidth = list(range(8)) 
#^product on this will return generator of tuples with all x,y combinations 
# which you will see bellow 

# place your images into dictionary as keys with dictionaries as value 
images_source = { 
    Img1: {} 
} 

for image in images_source: 
    for x, y in product(ImgHeight, ImgWidth): 
     r, g, b = image.getpixel((x, y)) 
     images_source[image][x, y] = [r, g, b] 

如你看到的 -它比多個嵌套循環更易讀。 我用隨機生成的值,並在這裏我已經有了:

{'Img1': {(0, 0): [191, 82, 190], 
      (0, 1): [122, 119, 164], 
      (0, 2): [218, 225, 146], 
      (0, 3): [217, 34, 128], 
      (0, 4): [0, 135, 114], 
      (0, 5): [193, 224, 4], 
      (0, 6): [186, 210, 186], 
      (0, 7): [232, 149, 158], 
      (1, 0): [51, 113, 191], 
      (1, 1): [177, 175, 245], 
      (1, 2): [5, 219, 54], 
      (1, 3): [230, 165, 241], 
      (1, 4): [41, 48, 39], 
      (1, 5): [16, 16, 188], 
      (1, 6): [62, 133, 242], 
      (1, 7): [255, 237, 221], 
      (2, 0): [98, 159, 60], 
      (2, 1): [135, 132, 209], 
      (2, 2): [248, 171, 34], 
      (2, 3): [35, 155, 166], 
      (2, 4): [237, 63, 245], 
      (2, 5): [184, 189, 164], 
      (2, 6): [194, 68, 148], 
      (2, 7): [68, 34, 58], 
      (3, 0): [247, 128, 219], 
      (3, 1): [30, 142, 60], 
      (3, 2): [77, 66, 157], 
      (3, 3): [53, 164, 51], 
      (3, 4): [77, 52, 145], 
      (3, 5): [169, 113, 59], 
      (3, 6): [232, 204, 70], 
      (3, 7): [120, 40, 128], 
      (4, 0): [219, 156, 67], 
      (4, 1): [128, 168, 120], 
      (4, 2): [188, 8, 71], 
      (4, 3): [233, 213, 91], 
      (4, 4): [63, 34, 223], 
      (4, 5): [145, 137, 84], 
      (4, 6): [112, 193, 111], 
      (4, 7): [50, 143, 19], 
      (5, 0): [5, 119, 61], 
      (5, 1): [168, 84, 28], 
      (5, 2): [211, 14, 73], 
      (5, 3): [251, 155, 64], 
      (5, 4): [199, 211, 213], 
      (5, 5): [135, 41, 121], 
      (5, 6): [163, 3, 57], 
      (5, 7): [2, 50, 167], 
      (6, 0): [66, 20, 212], 
      (6, 1): [55, 0, 229], 
      (6, 2): [76, 61, 47], 
      (6, 3): [241, 234, 249], 
      (6, 4): [203, 7, 130], 
      (6, 5): [216, 188, 202], 
      (6, 6): [58, 31, 136], 
      (6, 7): [191, 248, 197], 
      (7, 0): [109, 132, 16], 
      (7, 1): [198, 176, 36], 
      (7, 2): [138, 98, 215], 
      (7, 3): [255, 38, 206], 
      (7, 4): [73, 59, 202], 
      (7, 5): [192, 61, 89], 
      (7, 6): [148, 30, 233], 
      (7, 7): [68, 126, 154]}} 

沒有存儲字典座標與拉鍊值:

from itertools import product 

ImgHeight = list(range(3)) 
ImgWidth = list(range(3)) 

images_source = { 
    'Img1': [], 
    'Img2': [], 
} 

for image in images_source: 
    for x, y in product(ImgHeight, ImgWidth): 
     r, g, b = image.getpixel((x, y)) 
     images_source[image].append([r, g, b]) 

zipped_values = list(zip(*images_source.values())) 

和數據是:

# zipped_values      # x y 
[([216, 123, 197], [178, 214, 35]), # 0 0 
([2, 115, 179], [216, 188, 103]), # 0 1 
([102, 62, 255], [113, 69, 218]), # 0 2 
([188, 31, 59], [160, 117, 154]), # 1 0 
([255, 115, 10], [114, 142, 238]), # 1 1 
([64, 176, 189], [228, 10, 85]), # 1 2 
([121, 76, 202], [53, 217, 149]), # 2 0 
([114, 159, 18], [9, 202, 139]), # 2 1 
([146, 178, 59], [5, 216, 172])] # 2 2 

# images_source 
{'Img1': [[178, 214, 35],   # 0 0 
      [216, 188, 103],   # 0 1 
      [113, 69, 218],   # 0 2 
      [160, 117, 154],   # 1 0 
      [114, 142, 238],   # 1 1 
      [228, 10, 85],    # 1 2 
      [53, 217, 149],   # 2 0 
      [9, 202, 139],    # 2 1 
      [5, 216, 172]],   # 2 2 

'Img2': [[216, 123, 197],   # 0 0 
      [2, 115, 179],    # 0 1 
      [102, 62, 255],   # 0 2 
      [188, 31, 59],    # 1 0 
      [255, 115, 10],   # 1 1 
      [64, 176, 189],   # 1 2 
      [121, 76, 202],   # 2 0 
      [114, 159, 18],   # 2 1 
      [146, 178, 59]]}   # 2 2 

所以,你可以迭代zipped_values:

for i, coordinate in enumerate(product(ImgHeight, ImgWidth)): 
    colors = zipped_values[i] 
    x, y = coordinate 
    # do your stuff here 
    # average example: 
    # average_rgb = [sum(i)//len(colors) for i in zip(*colors)] 
與以前的數據

平均RGB每座標:

[197, 168, 116] # 0 0 
[109, 151, 141] # 0 1 
[107, 65, 236] # 0 2 
[174, 74, 106] # 1 0 
[184, 128, 124] # 1 1 
[146, 93, 137] # 1 2 
[87, 146, 175] # 2 0 
[61, 180, 78] # 2 1 
[75, 197, 115] # 2 2 

此外,您還可以創建其他字典和每個像素的顏色直接追加到它:

from itertools import product 

ImgHeight = list(range(3)) 
ImgWidth = list(range(3)) 

images_source = [Img1, Img2] 
new_image = {} 

for image in images_source: 
    for x, y in product(ImgHeight, ImgWidth): 
     r, g, b = image.getpixel((x, y)) 
     new_image.setdefault((x,y), []) 
     new_image[x,y].append([r, g, b]) 

for coordinate, colors in new_image.items(): 
    x, y = coordinate 
    # do your stuff here 
    # average example: 
    # average_rgb = [sum(i)//len(colors) for i in zip(*colors)] 

new_image結構:

{(0, 0): [([178, 214, 35], [216, 123, 197])], 
(0, 1): [([216, 188, 103], [2, 115, 179])], 
(0, 2): [([113, 69, 218], [102, 62, 255])], 
(1, 0): [([160, 117, 154], [188, 31, 59])], 
(1, 1): [([114, 142, 238], [255, 115, 10])], 
(1, 2): [([228, 10, 85], [64, 176, 189])], 
(2, 0): [([53, 217, 149], [121, 76, 202])], 
(2, 1): [([9, 202, 139], [114, 159, 18])], 
(2, 2): [([5, 216, 172], [146, 178, 59])]} 
+0

是的,這是真的我的代碼看起來有點不可讀。我想覆蓋九個不同的圖像,並獲得每個像素的中值顏色。 最後它唯一的一張圖片。這可以消除異常值,並可以從圖像中減去不需要的元素。 –

+0

@JonasDeichelmann檢查我的答案中的另一個例子,也許它會更適合您的要求 –

0

我改變了循環結構並改變了列表。 我的代碼:

ImgWidth, ImgHeight = Img1.size 
canvas = Image.new("RGB", (ImgWidth, ImgHeight), "white") 
Images = [Img1, Img2, Img3, Img4, Img5, Img6, Img7, Img8, Img9] 
i = len(Images) 

for source_x in range(ImgWidth): 
    for source_y in range(ImgHeight): 
     tmpRGBList = [0,0,0] 
     for tmpChannel in range(3): 
      tmpColorList = [] 
      for tmp_int in range(i): 
       tmpPixel = Images[tmp_int].getpixel((source_x,source_y)) 
       tmpColorList.append(tmpPixel[tmpChannel]) 
       if tmp_int == 8: 
        tmpColorList=sorted(tmpColorList) 
        tmpRGBList[tmpChannel] = tmpColorList[4] 
     canvas.putpixel((source_x,source_y), (tmpRGBList[0], tmpRGBList[1], tmpRGBList[2])) 
canvas.save("final.png") 

此功能現在。 感謝您的支持!