我怎样才能将 jpg 图像生成的所有哈希值保存到一个 csv 文件中,而不仅仅是最后一个?

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

在这里,您要为循环中的每个步骤覆盖 list_rows 变量。您应该附加到列表,然后将列表的内容写入您的 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:尽量不要重写内置函数(比如 hash) that may be dangerous !