I am trying to write code that will iterate over a directory of images and tell me which images are over 50% black - so I can get rid of those. This is what I have, but it isn't returning any results:
from PIL import Image
import glob
images = glob.glob('/content/drive/MyDrive/cropped_images/*.png') #find all filenames specified by pattern
for image in images:
with open(image, 'rb') as file:
img = Image.open(file)
pixels = list(img.getdata()) # get the pixels as a flattened sequence
black_thresh = (50,50,50)
nblack = 0
for pixel in pixels:
if pixel < black_thresh:
nblack += 1
n = len(pixels)
if (nblack / float(n)) > 0.5:
print(file)