话不多说,直接亮出代码; 关键语句在代码中有注释。 环境:python3.6
from PIL import Image
import numpy as np
import glob
def ChaZhi(pngfile):
maxmium = 0
minnium = 255
img = Image.open(pngfile)
try:
img.getdata()
img.convert("L")
img = np.array(img)
for i in range(len(img)):
for j in range(len(img[0])):
if img[i][j][0] > maxmium:
maxmium = img[i][j][0]
if img[i][j][0] < minnium:
minnium = img[i][j][0]
except Exception as e:
print(e)
return maxmium - minnium
def main():
i = 0
m = 0
for pngfile in glob.glob("D:\\~~你的文件夹路径~~\\*.png"):
i = i + 1
print('OK'+ str(i))
k = ChaZhi(pngfile)
print('max'+str(i)+"="+str(k))
if m < k:
m = k
print('max'+"="+str(m))
if __name__ == '__main__':
main()
如有疑问请不吝赐教。 另外,想获得更多好内容与资源,请移步微信公众号
|