import os
from PIL import Image,ImageStat
os.chdir('C:\\Users\\习习中\\desktop\\images')
sum_mean=0
sum_std=0
sum_files=0
for file in range(len(os.listdir())):
os.chdir(os.path.join(os.getcwd(),os.listdir()[file]))
for name in range(len(os.listdir())):
img=Image.open(os.listdir()[name])
stat=ImageStat.Stat(img)
sum_mean+=stat.mean[0]
sum_std+=stat.stddev[0]
sum_files+=len(os.listdir())
os.chdir(os.path.abspath(os.path.join(os.getcwd(), "..")))
mean = sum_mean/sum_files
std = sum_std/sum_files
print(mean,std)
|