0

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)
Hannah
  • 3
  • 2
  • What do you get if you print `nblack` and `n`? – mkrieger1 Feb 09 '23 at 22:45
  • When I print ``nblack``, it posts multiple increments of a single number. When I print `n`, it prints 124416 over and over. – Hannah Feb 09 '23 at 22:51
  • And is `nblack` larger than 62208? – mkrieger1 Feb 09 '23 at 23:01
  • No, it starts at 0 and increments from there over every pixel. – Hannah Feb 09 '23 at 23:05
  • You seem to assume all your images will be RGB, whereas some may be greyscale or palette images https://stackoverflow.com/a/52307690/2836621 You also seem to be trying to compare all three RGB channels with black (50,50,50) in one go in your `if` statement. – Mark Setchell Feb 09 '23 at 23:22

1 Answers1

0

Hannah, tuple comparison in python might not be what you expect: How does tuple comparison work in Python? . It is element by element, only for tiebreaking. Perhaps your definition of over 50% black is not what you have mapped to code?

I ran the above code on a dark image and my file printed just fine. Example png: https://www.nasa.gov/mission_pages/chandra/news/black-hole-image-makes-history .

Recommendations: define over 50% black to something you can put in code, pull the n out of the for loop, add files to a list when they satisfy your criteria.

lkel
  • 16
  • 2