Skip to content Skip to sidebar Skip to footer

Why Is Image.putpixel() Not Considered A Good Option For Changing Extensive Pixel Colors?

The documentation of Image.putpixel() states:- Modifies the pixel at the given position. The color is given as a single numerical value for single-band images, and a tuple for mul

Solution 1:

If you want six apples, it's way faster to go to the store once and buy six apples, than to go to the store six times and buy a single apple each time. The same is true for setting pixels.

Look at what the putpixel method is doing:

  1. It does various checks like whether the image is read-only, whether the implementation uses pyaccess, and which color mode the image uses
  2. It may check and verify the type and dimension of the pixel data
  3. self.im.putpixel undoubtedly does the same to the coordinates, and multiples to find the offset
  4. Finally it can do the actual operation: setting four bytes worth of pixel data.

That's dozen of operations, property accesses and method calls for a very small operation. If you call putpixel multiple times, it repeats all those operations every single time.

If you ask it to do multiple pixels at the same time, it can skip steps 1-3 for the next pixels because it'll be the same.

Post a Comment for "Why Is Image.putpixel() Not Considered A Good Option For Changing Extensive Pixel Colors?"