How to put loop result in csv file

Joined
Jan 3, 2023
Messages
1
Reaction score
0
i've done got my outputs for the csv file, but i dont know how to write it into csv file `
`` def find_mode(np_array) :
vals,counts = np.unique(np_array, return_counts=True)
index = np.argmax(counts)
return(vals[index])
folder = ("C:/Users/ROG FLOW/Desktop/Untuk SIDANG TA/Sudah Aman/testbikincsv/folderdatacitra/*.jpg")
for file in glob.glob(folder):
a = cv2.imread(file)
rows = a.shape[0]
cols = a.shape[1]
middlex = cols/2
middley = rows/2
middle = [middlex,middley]
titikawalx = middlex - 10
titikawaly = middley - 10
titikakhirx = middlex + 10
titikakhiry = middley + 10
crop = a[int(titikawaly):int(titikakhiry), int(titikawalx):int(titikakhirx)]
c = cv2.cvtColor(crop, cv2.COLOR_BGR2HSV)
H,S,V = cv2.split(c)
hsv_split = np.concatenate((H,S,V),axis=1)
Modus_citra = (find_mode(H)) ```
my outputs is modus citra, im trying to put it on csv file but im still confused how to write it into csv because the output inside a loop. Can someone help me how to write it into csv file ? i appreciate every help
 
Joined
Jan 30, 2023
Messages
107
Reaction score
13
i've done got my outputs for the csv file, but i dont know how to write it into csv file `
`` def find_mode(np_array) :
vals,counts = np.unique(np_array, return_counts=True)
index = np.argmax(counts)
return(vals[index])
folder = ("C:/Users/ROG FLOW/Desktop/Untuk SIDANG TA/Sudah Aman/testbikincsv/folderdatacitra/*.jpg")
for file in glob.glob(folder):
a = cv2.imread(file)
rows = a.shape[0]
cols = a.shape[1]
middlex = cols/2
middley = rows/2
middle = [middlex,middley]
titikawalx = middlex - 10
titikawaly = middley - 10
titikakhirx = middlex + 10
titikakhiry = middley + 10
crop = a[int(titikawaly):int(titikakhiry), int(titikawalx):int(titikakhirx)]
c = cv2.cvtColor(crop, cv2.COLOR_BGR2HSV)
H,S,V = cv2.split(c)
hsv_split = np.concatenate((H,S,V),axis=1)
Modus_citra = (find_mode(H)) ```
my outputs is modus citra, im trying to put it on csv file but im still confused how to write it into csv because the output inside a loop. Can someone help me how to write it into csv file ? i appreciate every help
You can use the csv module to write the results of the find_mode function to a CSV file.

Here a nice python script for it :

Python:
import csv

def find_mode(np_array) :
    vals,counts = np.unique(np_array, return_counts=True)
    index = np.argmax(counts)
    return(vals[index])

folder = ("C:/Users/ROG FLOW/Desktop/Untuk SIDANG TA/Sudah Aman/testbikincsv/folderdatacitra/*.jpg")

# Create a new CSV file or overwrite if it already exists
with open('modus_citra.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    # Write the header row
    writer.writerow(["Filename", "Modus Citra"])
    for file in glob.glob(folder):
        a = cv2.imread(file)
        rows = a.shape[0]
        cols = a.shape[1]
        middlex = cols/2
        middley = rows/2
        middle = [middlex,middley]
        titikawalx = middlex - 10
        titikawaly = middley - 10
        titikakhirx = middlex + 10
        titikakhiry = middley + 10
        crop = a[int(titikawaly):int(titikakhiry), int(titikawalx):int(titikakhirx)]
        c = cv2.cvtColor(crop, cv2.COLOR_BGR2HSV)
        H,S,V = cv2.split(c)
        hsv_split = np.concatenate((H,S,V),axis=1)
        modus_citra = find_mode(H)
        # Write a row for each file
        writer.writerow([file, modus_citra])
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top