#! python3
# 在硬盘上实别照片文件夹.py
import os
from PIL import Image
for foldername, subfolders, filenames in os.walk('.'):
numPhotoFiles = 0
numNonPhotoFiles = 0
for filename in filenames:
# Check if file extension isnt .png or .jpg
if not (filename.endswith('.png') or filename.endswith('.jpg')):
numNonPhotoFiles += 1
continue
# Open image file using Pillow.
imgObj = Image.open(os.path.join(foldername,filename))
# Check if width & height are larger than 500.
if imgObj.width * imgObj.height > 3000:
# Image is large enough to be considered a photo.
numPhotoFiles += 1
else:
# Image is too small to be a photo.
numNonPhotoFiles += 1
# If more than half of files were photos,
# Print the absolute path of the folder.
if numPhotoFiles > numNonPhotoFiles:
print(os.path.abspath(foldername))
要注意open的路径最好是绝对路径哦