Ananda
Ananda

Reputation: 3270

Image Processing in python using PIL

I am trying to do some image processing in Python using PIL. I need to raise a flag is the picture is has red colour in it. Can someone please give me some pointers?

I figured one can use split function on an image and split it into the individual channels. After this, I am not sure what to do.

Upvotes: 1

Views: 2026

Answers (1)

Hugo
Hugo

Reputation: 29364

Try something like this. It iterates over each pixel and checks if it's the one you want.

from PIL import Image
desired_colour = (255, 0, 0)
im = Image.open("myfile.jpg")
w, h = im.size
pix = im.load()
found = False
for i in range(w):
    for j in range(h):
        if pix[i, j] == desired_colour:
            # Bingo! Found it!
            found = True
            break

Upvotes: 1

Related Questions