Tazim Taz
Tazim Taz

Reputation: 117

Read images from two different folders and list the image directories with name in a csv file

There are two image folders names: bad_pictures and good_pictures. Two folders represent two different classes. I am trying to write a python script to make a CSV/JSON file which will contain two columns, one is CONTENT( full path of the images) and another one is LABEL( bad or good). Like:

enter image description here

As far I can remember that we may get the images from a single folder with:

from PIL import Image
import glob
image_list = []
for filename in glob.glob('yourpath/*.jpg'): 
    im=Image.open(filename)
    image_list.append(im)

which is far from my goal. I am hopping your help to figure our the codes to reach my goal.

Upvotes: 0

Views: 860

Answers (1)

Hetal Thaker
Hetal Thaker

Reputation: 717

Please check below to generate csv file:

import csv
import glob
path = "d:/so/good_pictures/*.*"
gp=glob.glob(path)
path = "d:/so/bad_pictures/*.*"
bp=glob.glob(path)
with open('test2.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(["CONTENT", "LABEL"])
    for element in gp:
        writer.writerow([element, "good"])
    for element in bp:
        writer.writerow([element, "bad"])

Upvotes: 1

Related Questions