2

Trying to read several images from a local folder and save them to csv using numpy.savetxt. Using following code

filelist = glob.glob('Filepath/*.png')
x = np.array([np.array(Image.open(fname)) for fname in filelist])
print(x.shape)
np.savetxt('csvfile.csv', x,fmt='%s')

Want this code to save one image as one row in the csv in one column. But this is storing array in 2 columns and not seperating the array to new line. As in the image second array is stored just after the first one and not to the new row.

[252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252] [252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252...

2
  • Because csv is text format, it would be more useful if you post it as text content. See this Commented Jan 20, 2018 at 7:05
  • @user202729 i that ok? Commented Jan 20, 2018 at 7:14

1 Answer 1

1


I think you could try with glob.glob, what should help


import numpy as np
import glob
import cv2
import csv

Libraries ⬆️; You know what⬇️

image_list = []

for filename in glob.glob(r'C:\your path to\file*.png'):    # '*' will count files each by one
    
    #Read
    img = cv2.imread(filename)
    flattened = img.flatten() 
    print(flattened) # recommend to avoid duplicates, see files and so on.

    #Save
    with open('output2.csv', 'ab') as f: #ab is set 
            np.savetxt(f, flattened, delimiter=",")

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

Comments

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.