openCV - Differentiate RGB and P&B photos into a directory

Asked

Viewed 51 times

0

Hello, Good morning.

It has a directory (linux) with thousands of photos, and I need to separate color from black and white. I created the algorithm to scan all the files and move them to a specific folder. However, I am not able to create the condition to check whether the image is colored or not. Could someone give me a solution? Thank you

  • 1

    In this case, can you read the color of the pixels in the photo? Or have you thought about how to compare the color code?

  • Hello! It would be interesting for you to read this article on how to ask a good question, so that you have help more quickly and in a targeted way. https://answall.com/help/how-to-ask

  • If you can read the rgb value of pixels, give to do 3 if’s in the rgb code and know if the color tone is closer to black, and then make the three if’s pro white color tone. And repeat this for each pixel within the image.

  • Hello, Edward Ramos. Thank you very much for the answer. I think this solution will be valid, I will test. I was looking for some function already ready that did it automatically, but I did not find.

1 answer

1


I found a way to check with the module PIL ImageStat (Here).

from PIL import Image, ImageStat

MONOCHROMATIC_MAX_VARIANCE = 0.005
COLOR = 1000
MAYBE_COLOR = 100

def detect_color_image(file):
    v = ImageStat.Stat(Image.open(file)).var
    is_monochromatic = reduce(lambda x, y: x and y < MONOCHROMATIC_MAX_VARIANCE, v, True)
    print file, '-->\t',
    if is_monochromatic:
        print "Monochromatic image",
    else:
        if len(v)==3:
            maxmin = abs(max(v) - min(v))
            if maxmin > COLOR:
                print "Color\t\t\t",
            elif maxmin > MAYBE_COLOR:
                print "Maybe color\t",
            else:
                print "grayscale\t\t",
            print "(",maxmin,")"
        elif len(v)==1:
            print "Black and white"
        else:
            print "Don't know..."

The COLOR and MAYBE_COLOR constants are quick options to find the differences between color and grayscale images, apparently not safe. As an example, some JPEG images that are seen as color but in real are grayscale with some color artifacts due to a scanning process.

  • Hello, Geilton. Thank you so much for your reply. I will also test.

  • I tested it here, and for my purposes, it worked well. Thank you, Geilton

  • @user157401 good that it worked, when possible accept the answer. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.