Home > Software design >  How can I convert multiple images to put them into a dataframe?
How can I convert multiple images to put them into a dataframe?

Time:02-17

I have a set of images with the same size. And I want to insert them into a dataframe, with the rows being the names of the images and the columns being the pixels. They are all in the same directory.

CodePudding user response:

Is this what you are looking for?

from matplotlib import image
import numpy as np
import pandas as pd

list_of_image_files = [
    'path/to/file1.png',
    'path/to/file2.png',
]

data = []
for file in list_of_image_files:
    img_arr = image.imread(file)
    img_arr = img_arr.flatten().reshape(-1, 1).T
    data.append({'file': file, 'image': img_arr})

df = pd.DataFrame(data)

Note that the image must be "readable" by numpy's imread method. If your image has a non-compatible file format then you still can use the pillow library; everything else stays the same.

CodePudding user response:

I want the pixels in the csv file to appear one per column and not all in the same column

Image 1

enter image description here

  • Related