Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defer parsing of palette into colors #6567

merged 1 commit into from Sep 24, 2022


Copy link

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.

@hugovk hugovk merged commit b7353af into python-pillow:main Sep 24, 2022
60 checks passed
@radarhere radarhere deleted the palette branch September 24, 2022 10:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

Successfully merging this pull request may close these issues.

None yet

2 participants