-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Apply dither when converting/quantizing to an adaptive palette #9859
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
Open
83Gh0st
wants to merge
1
commit into
python-pillow:main
Choose a base branch
from
83Gh0st:fix-adaptive-palette-dithering
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+96
−3
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1186,6 +1186,18 @@ def convert_transparency( | |||||
| new_im.palette = ImagePalette.ImagePalette( | ||||||
| "RGB", new_im.im.getpalette("RGB") | ||||||
| ) | ||||||
| if dither == Dither.FLOYDSTEINBERG: | ||||||
| # self.im.quantize() above has no dithering support, so the | ||||||
| # requested dither was silently dropped. Now that an adaptive | ||||||
| # palette has been computed, re-convert through it so the | ||||||
| # dithering actually takes effect. | ||||||
| # See https://github.com/python-pillow/Pillow/issues/5836 | ||||||
| im = self.im.convert("P", dither, new_im.im) | ||||||
| new_im = self._new(im) | ||||||
| new_im.palette = ImagePalette.ImagePalette( | ||||||
| "RGB", new_im.im.getpalette("RGB") | ||||||
| ) | ||||||
| assert new_im.palette is not None | ||||||
| if delete_trns: | ||||||
| # This could possibly happen if we requantize to fewer colors. | ||||||
| # The transparency would be totally off in that case. | ||||||
|
|
@@ -1272,7 +1284,7 @@ def quantize( | |||||
| method: int | None = None, | ||||||
| kmeans: int = 0, | ||||||
| palette: Image | None = None, | ||||||
| dither: Dither = Dither.FLOYDSTEINBERG, | ||||||
| dither: Dither | None = None, | ||||||
| ) -> Image: | ||||||
| """ | ||||||
| Convert the image to 'P' mode with the specified number | ||||||
|
|
@@ -1297,7 +1309,9 @@ def quantize( | |||||
| :param dither: Dithering method, used when converting from | ||||||
| mode "RGB" to "P" or from "RGB" or "L" to "1". | ||||||
| Available methods are :data:`Dither.NONE` or :data:`Dither.FLOYDSTEINBERG` | ||||||
| (default). | ||||||
| (default). Prior to this, dithering was silently ignored when no | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Reading this docstring on its own, I would have no idea what 'this' is. |
||||||
| ``palette`` argument was given (e.g. when quantizing to an adaptive | ||||||
| palette); it is now applied in that case too when requested. | ||||||
| :returns: A new image | ||||||
| """ | ||||||
|
|
||||||
|
|
@@ -1329,7 +1343,9 @@ def quantize( | |||||
| if self.mode not in {"RGB", "L"}: | ||||||
| msg = "only RGB or L mode images can be quantized to a palette" | ||||||
| raise ValueError(msg) | ||||||
| im = self.im.convert("P", dither, palette.im) | ||||||
| im = self.im.convert( | ||||||
| "P", Dither.FLOYDSTEINBERG if dither is None else dither, palette.im | ||||||
| ) | ||||||
| new_im = self._new(im) | ||||||
| assert palette.palette is not None | ||||||
| new_im.palette = palette.palette.copy() | ||||||
|
|
@@ -1347,6 +1363,16 @@ def quantize( | |||||
| palette_data = im.im.getpalette(mode)[: colors * len(mode)] | ||||||
| im.palette = ImagePalette.ImagePalette(mode, palette_data) | ||||||
|
|
||||||
| if dither == Dither.FLOYDSTEINBERG: | ||||||
| # self.im.quantize() above has no dithering support, so the | ||||||
| # requested dither was silently dropped. Now that a palette has | ||||||
| # been computed, re-convert through it so the requested | ||||||
| # dithering is actually applied. | ||||||
| # See https://github.com/python-pillow/Pillow/issues/5836 | ||||||
| dithered = self._new(self.im.convert("P", dither, im.im)) | ||||||
| dithered.palette = im.palette | ||||||
| return dithered | ||||||
|
|
||||||
| return im | ||||||
|
|
||||||
| def copy(self) -> Image: | ||||||
|
|
||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I appreciate that you're being backwards compatible, but doesn't this mean that the docstring of
should be updated?