I have split an image into multiples patches (pieces):
However, I want now to remove the black patches that contains more than 90% of black pixels. For the moment, I wrote this piece of code but it doesn't work because it increments the values of the black pixels:
import os
import glob
from PIL import Image
def count(path):
black = nonblack = 0
for filename in glob.glob(path):
img = Image.open(filename)
width = img.width
height = img.height
total = width * height
lower = 90
higher = 100
for pixel in img.getdata():
if pixel == (0, 0, 0):
black += 1
percent = round((black * 100.0/total),1)
if((percent >= lower) & (percent < higher)):
print('yes remove')
os.remove(filename)
break
else:
nonblack += 1
print("black = ", black, " nonblack = ", nonblack)
path = 'C:\\Mypath\\*.jpg'
count(path)