Do not convert images unnecessarily in ImageEnhance by radarhere · Pull Request #8431 · python-pillow/Pillow
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.