-1

I am currently working on a project. For this I have taken pictures and saved them in a folder.

The created image files are always called 0a0e69f4-c3c9-11eb-908c.jpg. Now I want to convert the created images into a CSV file. I read the pictures with cv2.

image = cv2.imread("a0e69f4-c3c9-11eb-908c.jpg")

My question is how can I output the complete images to a csv file with a run method? If I manually enter the path of each image in the ``image = cv2.imread("Output_Images")``` it works. Since there are too many images, I want to convert the complete images into a CSV file with one run click. How can I do that?

Ashur Ragna
  • 59
  • 1
  • 8
  • 1
    Does this answer your question? [Importing images from a directory (Python) to list or dictionary](https://stackoverflow.com/questions/26392336/importing-images-from-a-directory-python-to-list-or-dictionary) – Pranav Hosangadi Jun 02 '21 at 18:00
  • 1
    Thats what we use functions for, if you repeat a process over and over. your parameter is the folder. then iterate over all files in that folder, and for each file do things (in your case converting them) – Aru Jun 02 '21 at 18:00
  • Thank you. How can I make it so that it only takes over certain pictures, e.g. I have three pictures called ``Test_1, Test_2, Test_3``. There are also other pictures in the folder, but they are called something else. I only want these three pictures to be transferred to a csv file and all other pictures to be ignored. – Ashur Ragna Jun 02 '21 at 18:06

4 Answers4

2

Just use a for loop and iterate over images in the directory:

import glob
for file in glob.iglob('Output_Images/*.jpg'):
        image = cv2.imread(file)
Anurag
  • 308
  • 2
  • 13
1

os.listdir('Output_Images') will give you a list of all the files in the directory, then use string concatenation or os.path.join() to build the paths dynamically via a for loop.

fsimonjetz
  • 5,644
  • 3
  • 5
  • 21
0

This python code converts jpg image folder to csv file. Well done

import os

from PIL import Image

import numpy as np

def ListofFiles(Dir):
    Files = []

    for root, dir_name, file_name in os.walk(Dir): 

#root store address till directory, dir_name stores directory name # file_name 
stores file name

        for name in file_name:

            fullName = os.path.join(root, name)

            Files.append(fullName)

    return Files

FileList = ListofFiles('C:/..../..../your_folder') # <----path

pixels=[]

for file in FileList:

    Im = Image.open(file)

    pixels.append(list(Im.getdata()))

from numpy import savetxt

pixels_arr=np.asarray(pixels)

print((pixels_arr.shape))

#savetxt('numbers.csv', pixels_arr, delimiter=',')

np.savetxt('pred1.csv', pixels_arr, fmt='%s')
cigien
  • 57,834
  • 11
  • 73
  • 112
-1
import os
import matplotlib.pyplot as plt
def load_img(name):
    img = plt.imread(name)
    return img
images = [load_img(im) for im in os.listdir(Path)]
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57