-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?

  • 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

3 Answers3

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.k
  • 284
  • 1
  • 12
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
  • 3,293
  • 1
  • 4
  • 17
-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
  • 31,792
  • 14
  • 29
  • 50