Sep-24-2023, 03:22 PM
You should avoid using for loops for things like image processing. They are very slow compared to calling functions in numpy or cv2.
A way to count non-white pixels using numpy.
A way to count non-white pixels using numpy.
import numpy as np from PIL import Image # Make an array of pixels pixels = np.array(Image.open("test.png"))[:, :, :3].reshape(-1, 3) # Add up the RGB values pixel_sums = np.sum(pixels, axis=1) # Count number of pixels with sum < 765 not_white = (np.count_nonzero(pixel_sums < 765))