-1

In my program I current create a numpy array full of zeros and then for loop through each element replacing it with the desired value. Is there a more efficient way of doing this?

Below is an example of what I am doing however, instead of a int I have a list of each row which needs put into the numpy array. Is there a way to put replace whole rows and is that more efficient.

import numpy as np
from tifffile import imsave

image = np.zeros((5, 2160, 2560), 'uint16')

num =0
for pixel in np.nditer(image, op_flags=['readwrite']):
     pixel = num
     num += 1
imsave('multipage.tif', image)
4

2 に答える 2

1

スライスを使用して行全体に割り当てるだけです

import numpy as np
from tifffile import imsave

list_of_rows = ... # all items in list should have same length
image = np.zeros((len(list_of_rows),'uint16')

for row_idx, row in enumerate(list_of_rows):
    image[row_idx, :] = row

imsave('multipage.tif', image)

Numpy slicing は非常に強力で優れています。このドキュメントを読んで、何が可能かを理解することをお勧めします。

于 2013-10-24T11:49:51.600 に答える