1

I have a list which I created after appending the images from a folder

samples=[]    
for filename in glob.glob(path + '/*.png'):
    samples.append(misc.imread(filename))

And a sample of the list looks like

[array([[ 4,  4,  4, ...,  5,  5,  4],
   [ 5,  5,  5, ...,  6,  6,  5],
   [ 5,  5,  5, ...,  6,  6,  4],
   ..., 
   [12, 12, 11, ..., 12, 12,  7],
   [12, 11, 11, ..., 13, 12,  7],
   [11, 11, 10, ..., 12, 12,  7]], dtype=uint8), array([[ 4,  4,  4, ...,  7,  7,  6],
   [ 5,  5,  5, ...,  6,  6,  4],
   [ 5,  5,  5, ...,  7,  7,  5]], dtype=uint8)]

How it convert the image's dimensions in a Pandas DataFrame. when I tried to do it with

df=pd.DataFrame(samples)

It gives me an error

ValueError: Must pass 2-d input

Please Suggest- I will appreciate every help

Manu Sharma
  • 1,593
  • 4
  • 25
  • 48
  • You have a list of 2-d arrays which makes `samples` a 3-d object. Look into Pandas' panels which are designed to hold 3-d data. – Khris Aug 26 '16 at 12:21
  • i want to convert into a 2d dataframe only, so that I can use isomaps techniques going ahead – Manu Sharma Aug 26 '16 at 12:23
  • Then you need to put each element of the list into its own dataframe or create a dataframe with the inner lists as elements in your cells. – Khris Aug 26 '16 at 13:26
  • Do you want to "flatten" each image? That is, do you want to reshape it from a two-dimensional array with shape `(m, n)` to a 1-d array with length `m*n`? If so, do you want the data for each image to be a row or a column in the DataFrame? – Warren Weckesser Aug 26 '16 at 13:38
  • A more fundamental question: why do you want to use a pandas DataFrame? – Warren Weckesser Aug 26 '16 at 13:47
  • Will this do: `df = pd.DataFrame(np.vstack(samples))`? – Nickil Maveli Aug 26 '16 at 14:37

2 Answers2

0

Try converting it into a pandas panel

import cv2
img = cv2.imread('path/Picture_1.png')
imgPanel = pd.Panel(img)

For more info see: Panel- Introduction, n-dimensional Panel (Experimental), Cookbook- Panel, Panel-nd cookbook

Abhijay Ghildyal
  • 4,044
  • 6
  • 33
  • 54
0

Try adding .reshape(-1) while appending an image.

for filename in glob.glob(path + '/*.png'):
    samples.append(misc.imread(filename).reshape(-1))

df = pd.DataFrame.from_records(samples)

If you want to 3d version please read Reshaping a numpy array in python

Community
  • 1
  • 1
AnandShanbhag
  • 6,389
  • 1
  • 20
  • 20