So I'm using an image's pixel data, applying some calculations to it, which gives me a resulting array whose shape is unknown until the calculations are done to it. I'm having trouble reshaping the outputted array into a 2-dimensional or 3-dimensional array
Here is an example
from PIL import Image
img = Image.open('C:\Users\Amit\Desktop\sample_pic.jpeg').convert("RGB")
pixels =np.array(img)
print(pixels.shape) # (477L, 887L, 3L) PIL seems to switch height and width because original dimensions are 877 x 477
flat = pixels.flatten()
print (flat.shape) # (1269297L,)
filter1= np.array([1,1,0])
pixels2 = np.array([])
for i in range(0, len(flat),2):
pixels2 =np.append(pixels2,np.sum((flat[i:i+3] * filter1)))
The loop at the end is just doing some calculations to the flattened array, and outputting a new array whose shape I don't know till the output
print pixels2.shape
#(634649L,)
So I'm trying to reshape the outputted array into dimensions fit for a picture. I tried the code
pixels2.reshape(800,-1)
but I got the error
pixels2.reshape(800,-1)
ValueError: total size of new array must be unchanged
same with
pixels.reshape(800,-1,3)
ValueError: total size of new array must be unchanged
I was hoping that adding the (-1) would automatically find the appropriate second dimension but that doesn't seem to be the case. I'm not bound to the number 800 as one of the dimensions but I'm looking for the first two dimensions to be above 300 so (300+, 300+, 3)
Thanks.
Update:
adding one more element to the pixels2
array, makes it a (634650L,) array which is divisible by 3. ( I found it by trial and error)
But finding the other two dimensions involves a lot of trial and error as well it seems. (800, -1, 3) doesn't work.