Python 3.6:附加3D列表

时间:2021-09-21 19:54:58

My Code:

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])

I want to save the color channel of each pixel in a separate list from each image in the list. For this I have a 3D list. I got the error Message:

我想将每个像素的颜色通道保存在列表中每个图像的单独列表中。为此,我有一个3D列表。我收到错误消息:

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

AttributeError:'int'对象没有属性'insert'

When I use

我用的时候

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

Output:

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

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

2 个解决方案

#1


1  

I would suggest you to use something like this:

我建议你使用这样的东西:

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]

As you can see - it's better readable than multiple nested loops. I used random to generate values, and here what i've got:

如您所见 - 它比多个嵌套循环更易读。我使用随机生成值,在这里我得到了:

{'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]}}

Without storing coordinates in dictionary with values zipped:

不用压缩值存储字典中的坐标:

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()))

And data would be :

数据将是:

# 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

So, you could iterate over zipped_values with:

因此,您可以使用以下内容迭代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)]

Average rgb per coordinate with previous data:

与先前数据的每个坐标的平均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

Also, you can create another dictionary and append each pixel color to it directly:

此外,您可以创建另一个字典并直接将每个像素颜色附加到它:

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 structure:

{(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])]}

#2


1  

I changed the loop Structur and changed the List. My Code:

我更改了循环Structur并更改了List。我的代码:

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")

This works now. Thanks for your support!

这现在有效。感谢您的支持!

#1


1  

I would suggest you to use something like this:

我建议你使用这样的东西:

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]

As you can see - it's better readable than multiple nested loops. I used random to generate values, and here what i've got:

如您所见 - 它比多个嵌套循环更易读。我使用随机生成值,在这里我得到了:

{'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]}}

Without storing coordinates in dictionary with values zipped:

不用压缩值存储字典中的坐标:

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()))

And data would be :

数据将是:

# 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

So, you could iterate over zipped_values with:

因此,您可以使用以下内容迭代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)]

Average rgb per coordinate with previous data:

与先前数据的每个坐标的平均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

Also, you can create another dictionary and append each pixel color to it directly:

此外,您可以创建另一个字典并直接将每个像素颜色附加到它:

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 structure:

{(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])]}

#2


1  

I changed the loop Structur and changed the List. My Code:

我更改了循环Structur并更改了List。我的代码:

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")

This works now. Thanks for your support!

这现在有效。感谢您的支持!