は、私はあなたがこのようなものを使用することをお勧めご覧のように -複数のネストされたループよりも読みやすくなります。
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
{'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]}}
zip形式の値で辞書内の座標を格納することなく: は、私は私が持っているもの、ここで値を生成するためにランダムに使用し、
したがって、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'です。私は、あなたの画像が細かい配列であると仮定します。その場合、より高いレベルで何をしようとしているのか、場合によっては3x3画像の入力と出力のサンプルを説明する必要があります。これを解決する簡単な方法があるはずです。 –