0

I have split an image into multiples patches (pieces):

Exemple of an image splited into 16 patches

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)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • Have a look at [`Image.getcolors`](https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.getcolors). For each color, you also get the amount of pixels with that color. Get that count for `(0, 0, 0)` and divide by `width * height` of the image. – HansHirse Apr 30 '21 at 11:54
  • I wouldn't mark as a duplicate because it doesn't have the percentage criteria, but highly related: [Python PIL Detect if an image is completely black or white](https://stackoverflow.com/q/14041562/6045800) – Tomerikoo Apr 30 '21 at 13:15

2 Answers2

0

You should do the percentage check after counting all the 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
                else:
                    nonblack += 1
                    print("black = ", black, " nonblack = ", nonblack)
            
            percent = round((black * 100.0/total),1)
            if((percent >= lower) & (percent < higher)):
                print('yes remove')
                os.remove(filename)
                break

    path = 'C:\\Mypath\\*.jpg'
    count(path)

However I feel there is an issue with your approach, because you are only counting pixels that are (0,0,0) as black pixels. A small variation in the RGB value would still appear black but would not be counted. For example (0,0,1), (1,3,4) etc...... all appear black but would not be counted by your code.

0

I removed all the images that contain 100% black pixels.

def count(path):
    for filename in glob.glob(path):
        img = Image.open(filename)
        
        if not img.getbbox():
            print('yes remove')
            os.remove(filename)
        else:
            print("Not to remove" + filename)
      
path = "D:\\UCC\\Project\\Code\\test_plot\\*.png"
count(path)