Reputation: 1044
I will first explain what I want to do. I have an image and I want to store the pixel values for a specific ROI. For that reason I implement the following loop(found in another topic of the site):
pixels = im.load()
all_pixels = []
for x in range(SpecificWidth):
for y in range(SpecificHeight):
cpixel = pixels[x, y]
all_pixels.append(cpixel)
However, it does not return a SpecificwidthXSpecificHeight matrix but one of length as much as the values. Because I want to keep the size of the matrix of the ROI I implement the following loop(much the same with the previous):
array=np.array(all_pixels)
roi_pixels = np.zeros((SpecificWidth,SpecificHeight))
for i in range(0,array.shape[0],width):
c_roi_pixels=all_pixels[i]
roi_pixels.append(c_roi_pixels)
And I have the error as it mentioned in the title.
Upvotes: 2
Views: 33403
Reputation: 74172
@RolandSmith is absolutely right about the cause of the error message you're seeing. A much more efficient way to achieve what you're trying to do is to convert the whole image to a numpy array, then use slice indexing to get the pixels corresponding to your ROI:
# convert the image to a numpy array
allpix = np.array(im)
# array of zeros to hold the ROI pixels
roipix = np.zeros_like(allpix)
# copy the ROI region using slice indexing
roipix[:SpecificHeight, :SpecificWidth] = allpix[:SpecificHeight, :SpecificWidth]
Upvotes: 2
Reputation: 43495
In numpy, append
is a function, not a method.
So you should use e.g:
roi_pixels = np.append(roi_pixels, c_roi_pixels)
Note that the append
function creates and returns a copy! It does not modify the original.
Upvotes: 4