57 lines
1.4 KiB
Python
57 lines
1.4 KiB
Python
# -*- coding:utf-8 -*-
|
||
"""
|
||
Author: qiaoxinjiu
|
||
Email: qiaoxinjiu@sparkedu.com
|
||
Create Date: 2022/05/08 5:58 下午
|
||
"""
|
||
import os
|
||
from PIL import Image
|
||
|
||
def image_gray(img):
|
||
# 打开图片
|
||
img = Image.open(img)
|
||
|
||
# 计算平均灰度值
|
||
gray_sum = 0
|
||
count = 0
|
||
for x in range(img.width):
|
||
for y in range(img.height):
|
||
if img.mode == "RGB":
|
||
r, g, b = img.getpixel((x, y))
|
||
gray_sum += (r + g + b) / 3
|
||
elif img.mode == "L":
|
||
gray_value = img.getpixel((x, y))
|
||
gray_sum += gray_value
|
||
count += 1
|
||
|
||
avg_gray = gray_sum / count
|
||
return avg_gray
|
||
|
||
|
||
def find_image(folder_path):
|
||
# 定义一个列表存储图片路径
|
||
images = []
|
||
# 遍历文件夹下的所有文件
|
||
for root, dirs, files in os.walk(folder_path):
|
||
for file in files:
|
||
file_path = os.path.join(root, file)
|
||
# 处理每个文件,将其添加到列表中
|
||
images.append(file_path)
|
||
return images
|
||
|
||
|
||
def assert_run(folder_path):
|
||
images = find_image(folder_path)
|
||
for img in images:
|
||
gray = image_gray(img)
|
||
# 灰度值小于50,将认为是黑图
|
||
if gray < 50:
|
||
print(img, ":", gray)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
# image_gray()
|
||
# find_image()
|
||
folder_path = r'D:\picture'
|
||
assert_run(folder_path)
|