I have this code that allows you to detect pixels of a vertain value. Right now I\'m detecting pixels over a certain value (27). My idea would be to still detect them but to det
A few ideas:
if, as you say, your images are greyscale, then you should process them as single-channel greyscale images, rather than needlessly tripling their memory footprint and tripling the number of comparisons you need to make by promoting them to RGB
rather than use nested for
loops which are miserably slow in Python, either use Numpy or OpenCV to gain 10x to 1000x speedup. Similar example here.
if you have many images to process, they are all independent, and you have a decent CPU and RAM, consider using multiprocessing to get all your lovely cores processing images in parallel. Simple example here.
The second suggestion is most likely to yield the best dividend, so I'll expand that:
from PIL import Image
import Numpy as np
# Open an image and ensure greyscale
im = Image.open('image.png').convert('L')
# Make into Numpy array
na = np.array(im)
# Make a new array that is True where pixels between 65..75 and False elsewhere
x = np.logical_and(na>65,na<75)
# Now count the True pixels
result = np.count_nonzero(x)
That yields 2,200 for this 400x100 image: