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

Do not convert images unnecessarily in ImageEnhance #8431

Merged
merged 1 commit into from
Oct 1, 2024

Conversation

radarhere
Copy link
Member

If an attempt is made to convert an image to its current mode (without a matrix), a copy is simply returned.

However, copy() still consumes resources, and it would be ideal to avoid it where possible. In ImageEnhance, there are a few such instances.

What if the image already matches self.intermediate_mode in Color?

self.degenerate = image.convert(self.intermediate_mode).convert(image.mode)

What if the image is already L mode in Enhance?

def __init__(self, image: Image.Image) -> None:
self.image = image
mean = int(ImageStat.Stat(image.convert("L")).mean[0] + 0.5)
self.degenerate = Image.new("L", image.size, mean).convert(image.mode)

This PR skips calling convert() for those scenarios.

@radarhere radarhere changed the title Do not convert image unnecessarily in ImageEnhance Do not convert images unnecessarily in ImageEnhance Sep 30, 2024
@hugovk hugovk merged commit 0e0cb06 into python-pillow:main Oct 1, 2024
48 checks passed
@radarhere radarhere deleted the imageenhance branch October 1, 2024 11:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants