Image pixels reshaping

Hello! From the suggested question does not seem like this has been asked before, so here it goes.
I have an image (of the classical shape image.shape = (height, width, 3)), and I want to stack together the pixels in order to put them in a Pandas dataframe with only one column for red, one for green and one for blue. So far I have:

r = []
g = []
b = []

for row in image:
    for pixel in row:
        temp_r, temp_g, temp_b = pixel
        r.append(temp_r)
        g.append(temp_g)
        b.append(temp_b)
        
r = whiten(r)
g = whiten(g)
b = whiten(b)

pixels = pd.DataFrame({'red':r,
                       'green':g,
                       'blue':b})

In[]: pixels.head()
Out[]: 
   red  green  blue
0   21     13     8
1   21     13     9
2   20     11     8
3   21     13    11
4   21     14     8

This of course, due to the for loop, is generating a huge bottleneck, and the image is only 600x400. Does anybody know if there is a more efficient way to do this?

Accrording to the following video, you can downright open an image as nd.array()
With that, it shouldn’t be to hard to reshape it into a format for a dataframe.
Also in the Tensorflow course a command “flatten” is mentioned, which does turn an image into the format you describe. But I’d need to check if it was a command for pandas or tensorflow.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.