Skip to content Skip to sidebar Skip to footer

Efficiently Extract First 5 Pixels From Image Using Pil

I have a large package of .jpg images of the sky, some of which are artificially white; these images are set to (255, 255, 255) for every pixel. I need to pick these images out.

Solution 1:

You can use the Image.getpixel method:

im = Image.open(imagepath)
ifall(im.getpixel((0, x)) == (255, 255, 255) for x inrange(5)):
    # Image is saturated

This assumes that your image has at least five pixels in each row.

Normally, accessing individual pixels is much slower than loading the whole image and messing around with a PixelAccess object. However, for the tiny fraction of the image you are using, you will probably lose a lot of time loading the entire thing.

You may be able to speed things up by calling load on a sub-image returned lazily by crop:

im = Image.open(imagepath).crop((0, 0, 5, 1)).load()
ifall(x == (255, 255, 255) for x in im):
    # Image is saturated

Post a Comment for "Efficiently Extract First 5 Pixels From Image Using Pil"