0

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.

data in csv

1
  • Did the solution helped or needs an edit? Commented May 26, 2020 at 15:40

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much! That helps. :D
Is there a way to load the images that are in the same row at once? I just have two columns.
can you provide sample output ? df.iloc[i,1] can load image from second column
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/

2 Comments

but that will just print the raw path that's present in the csv. Instead I want to read the image in the path.
This might be what you're looking for then: stackoverflow.com/questions/53468558/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.