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.
Asked
Active
Viewed 983 times
0
-
Did the solution helped or needs an edit? – SahilDesai May 26 '20 at 15:40
2 Answers
1
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.

SahilDesai
- 512
- 3
- 6
-
-
Is there a way to load the images that are in the same row at once? I just have two columns. – blackPanther May 26 '20 at 20:56
-
can you provide sample output ? df.iloc[i,1] can load image from second column – SahilDesai May 27 '20 at 04:10
0
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/

Andrew Halpern
- 514
- 3
- 6
-
but that will just print the raw path that's present in the csv. Instead I want to read the image in the path. – blackPanther May 26 '20 at 03:52
-
This might be what you're looking for then: https://stackoverflow.com/questions/53468558/adding-image-to-pandas-dataframe – Andrew Halpern May 26 '20 at 03:54