Pillow icon indicating copy to clipboard operation
Pillow copied to clipboard

Defer parsing of palette into colors

Open radarhere opened this issue 3 years ago • 0 comments

Addresses #6561

The discussion points out that Image.fromarray(a, "P") is slower than Image.fromarray(a, "L"). The difference is that when fromarray calls frombuffer, for P mode an ImagePalette instance is created. Once ImagePalette assigns the initial palette, it parses the palette into a colors dictionary. This parsing is the main difference in speed.

I'm not convinced that Image.fromarray(a, "P") is a common enough to warrant speed optimization. However, the parsing of the initial ImagePalette palette string would be an operation shared through all P mode images, and depending on the series of operations from the user, colors is not necessarily used afterwards.

This PR defers the parsing of the palette into colors until the colors attribute is accessed.

radarhere avatar Sep 09 '22 12:09 radarhere