I have a CSV containing n records and it is filled with absolute paths to the images. I'd like to import those images into a numpy matrix.
2 Answers
import pandas as pd
from PIL import Image
import numpy as np
def load_image( infilename ) :
img = Image.open( infilename )
img.load()
data = np.asarray( img, dtype="int32" )
return data
df = pd.read_csv (r'Path where the CSV file is stored\File name.csv')
for i in range(len(df)) :
print(load_image(df.iloc[i, 0]))
You can store the returned values in list if you want else directly use.
3 Comments
blackPanther
Thank you so much! That helps. :D
blackPanther
Is there a way to load the images that are in the same row at once? I just have two columns.
SahilDesai
can you provide sample output ? df.iloc[i,1] can load image from second column
You can use the pandas read_csv function.
import pandas as pd
df = pd.read_csv (r'Path where the CSV file is stored\File name.csv')
print (df)
Source: https://datatofish.com/import-csv-file-python-using-pandas/
2 Comments
blackPanther
but that will just print the raw path that's present in the csv. Instead I want to read the image in the path.
Andrew Halpern
This might be what you're looking for then: stackoverflow.com/questions/53468558/…
