Skip to content
Advertisement

How could i save all the hashes generated from jpg images into a csv file and not only the last one?

import imagehash
from PIL import Image
import glob
import numpy as np

image_list = []
for filename in glob.glob('/home/folder/*.jpg'):
    im=Image.open(filename)
    image_list.append(im)
    hash = imagehash.average_hash(im)
    print(hash)
    list_rows = [[hash]]
    np.savetxt("numpy_test.csv", list_rows, delimiter=",", fmt='% s')

how to save all the hashes generated into the same csv file and not only the last one

Advertisement

Answer

Here, you’re overwriting your list_rows variable for every step in the loop. You should append to the list instead, and then write the content of the list to your csv.

import imagehash
from PIL import Image
import glob
import numpy as np

image_list = []
list_rows = []

for filename in glob.glob('/home/folder/*.jpg'):
    im = Image.open(filename)
    image_list.append(im)
    img_hash = imagehash.average_hash(im)
    print(img_hash)
    list_rows.append([img_hash])

np.savetxt("numpy_test.csv", list_rows, delimiter=",", fmt='% s')

PS: Try not to override builtin (like hash) that may be dangerous !

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement