我会建议你使用这样的:
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])]}
请不要这样做:'tmp_list = [[[0] * 3] * ImgHeight] * ImgWidth'您正在复制参考。 –
好的,你的提示是什么? 我该如何做得更好? –
我的建议是使用'np.zeros'或'np.empty'。我假设你的图像是numpy数组。在这种情况下,你应该解释你想要在更高层次上做什么,可能还有一些样例输入和输出(比如说)3x3图像。应该有一个更简单的方法来解决这个问题。 –