JavaScript
x
14
14
1
import imagehash
2
from PIL import Image
3
import glob
4
import numpy as np
5
6
image_list = []
7
for filename in glob.glob('/home/folder/*.jpg'):
8
im=Image.open(filename)
9
image_list.append(im)
10
hash = imagehash.average_hash(im)
11
print(hash)
12
list_rows = [[hash]]
13
np.savetxt("numpy_test.csv", list_rows, delimiter=",", fmt='% s')
14
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.
JavaScript
1
17
17
1
import imagehash
2
from PIL import Image
3
import glob
4
import numpy as np
5
6
image_list = []
7
list_rows = []
8
9
for filename in glob.glob('/home/folder/*.jpg'):
10
im = Image.open(filename)
11
image_list.append(im)
12
img_hash = imagehash.average_hash(im)
13
print(img_hash)
14
list_rows.append([img_hash])
15
16
np.savetxt("numpy_test.csv", list_rows, delimiter=",", fmt='% s')
17
PS: Try not to override builtin (like hash) that may be dangerous !