Python Forum
Insert images in a folder into dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Insert images in a folder into dataframe
#1
Hi

Im trying to read images from folders into a dataframe , where each row in the dataframe is all the images for a folder :

import cv2

import os,glob
import matplotlib.pylab as plt
from os import listdir,makedirs
from os.path import isfile,join
import pandas as pd
import PIL
import numpy as np
from scipy.ndimage import imread

pth = 'C:/Users/sarmad/Documents/n/'

folders =  os.listdir(pth)



videos = pd.DataFrame()
for folder in folders:
    pth_upd = pth + folder + '/'
    #print(pth_upd)
    
    allfiles = os.listdir(pth_upd)
    files = []
    columns = ['data']
    index = [folders]
    for file in allfiles:
        files.append(file) if ('.bmp' in file) else None
    
    samples =  np.empty((0,64,64))
    for file in files:
        img = cv2.imread(os.path.join(pth_upd,file),cv2.IMREAD_GRAYSCALE)
        
        #print(img)
        # read image
        img = img.reshape(1,64,64)            
        samples = np.append(samples, img, axis=0)
        df = pd.DataFrame( index=folders)
        df['img_frames'] = pd.DataFrame([samples],index=folders)

  
I can read all the images in each folder to {samples} list but when I try to insert them into dataframe I get this error :

Quote:ValueError Traceback (most recent call last)
<ipython-input-99-db4b01fe9450> in <module>
20 samples = np.append(samples, img, axis=0)
21 df = pd.DataFrame( index=folders)
---> 22 df['img_frames'] = pd.DataFrame([samples],index=folders)
23
24

ValueError: Must pass 2-d input
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to insert data in a dataframe? man0s 1 1,326 Apr-26-2022, 11:36 PM
Last Post: jefsummers
  convert images into pixel dataframe into csv file using python synthex 3 17,499 Feb-17-2019, 06:26 AM
Last Post: scidam
  Assign image names in folder to images in other folder. ujjwalrathod007 15 7,786 Sep-03-2018, 05:27 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020