0

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.

Moondra
  • 4,399
  • 9
  • 46
  • 104
  • So you are looking for two integers ``x,y`` such that ``pixels.shape=(x,y,3)``, right? For that you would need that ``634649`` is divisible by 3 (634649=x*y*3), however it isn't. So it just won't work. – alexblae Jan 17 '17 at 08:56
  • Is there no way around this? What about a method that adds in extra data to make the dimensions work out? – Moondra Jan 17 '17 at 12:48
  • Well, it really depends what you want to do with the processed image. If the "position" of the pixel (x,y) really has any meaning. You could add a dummy number via ``pixels2=np.array(list(pixels2).append(dummyValue))``, then the whole thing is divisible by 3. – alexblae Jan 17 '17 at 12:53
  • Thanks for the reply. The added pixels won't have any meaning, just fillers. I was hoping for some sort of built-in method, which would make the process easier. I'm guess not. :( Thanks for the help! – Moondra Jan 17 '17 at 13:26
  • 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. – Moondra Jan 17 '17 at 14:27
  • 1
    Obviously 800 does not work, since ``634650/3`` is not divisible by 800. I don't know if there is a really elegant way to do this other than factorizing ``634650/3`` for example by this way http://stackoverflow.com/questions/6800193/what-is-the-most-efficient-way-of-finding-all-the-factors-of-a-number-in-python or simply looping down from 800 and trying out values, and as soon you it doesn't throw and error, use that value. – alexblae Jan 17 '17 at 14:52
  • Thanks. I had seen that factoring thread before, some of the code was a little above my head . I will review the code and just implement one of the functions from that thread. – Moondra Jan 18 '17 at 10:35

0 Answers0