Converting images to csv file in python

前端 未结 4 1535
耶瑟儿~
耶瑟儿~ 2020-12-03 12:51

I have converted my image into a csv file and it\'s like a matrix but I want it to be a single row. How can I convert all of the images in dataset into a csv file (each ima

4条回答
  •  甜味超标
    2020-12-03 13:18

    import numpy as np
    import cv2
    import os
    
    IMG_DIR = '/home/kushal/Documents/opencv_tutorials/image_reading/dataset'
    
    for img in os.listdir(IMG_DIR):
            img_array = cv2.imread(os.path.join(IMG_DIR,img), cv2.IMREAD_GRAYSCALE)
    
            img_array = (img_array.flatten())
    
            img_array  = img_array.reshape(-1, 1).T
    
            print(img_array)
    
            with open('output.csv', 'ab') as f:
    
                np.savetxt(f, img_array, delimiter=",")
    

提交回复
热议问题