Repository navigation
Add Jarvis-Judice-Ninke dithering #10265
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
hiyouwhatsapp
wants to merge
10
commits into
python-pillow:main
Choose a base branch
from
hiyouwhatsapp:feature/jjn-dithering-9619
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.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3a0eb6b
Add Jarvis-Judice-Ninke dithering for bilevel and palette images
hiyouwhatsapp 147af73
Tidy JJN test fixtures
hiyouwhatsapp cfd926a
Keep reference diffuser formatting consistent
hiyouwhatsapp 840ec17
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 99b2a06
Fix line length
radarhere 10160d2
Document new dither mode
radarhere af8ae34
Fix underline length
radarhere 59370d3
fix: name JJN dither mode and clarify error-buffer bounds
hiyouwhatsapp 02da160
test: add Hopper JJN reference image
hiyouwhatsapp 1825b52
test: compare JJN Hopper output to reference image
hiyouwhatsapp 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| P1 | ||
| # JJN-dithered crop (24, 24, 56, 56) of hopper.ppm | ||
| 32 32 | ||
| 0 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | ||
| 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1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 1 1 1 1 1 0 0 1 0 0 1 0 0 1 0 0 0 1 0 0 1 0 0 1 1 1 0 0 0 0 1 1 |
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 |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| """Jarvis-Judice-Ninke error diffusion for bilevel and palette images.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
|
|
||
| from PIL import Image | ||
|
|
||
| from .helper import assert_image_equal, hopper | ||
|
|
||
| # The JJN kernel has twelve forward neighbors and is normalized by 48. | ||
| TAPS = ( | ||
| (1, 0, 7), | ||
| (2, 0, 5), | ||
| (-2, 1, 3), | ||
| (-1, 1, 5), | ||
| (0, 1, 7), | ||
| (1, 1, 5), | ||
| (2, 1, 3), | ||
| (-2, 2, 1), | ||
| (-1, 2, 3), | ||
| (0, 2, 5), | ||
| (1, 2, 3), | ||
| (2, 2, 1), | ||
| ) | ||
|
|
||
|
|
||
| def _expected_bilevel(values: list[int], size: tuple[int, int]) -> list[int]: | ||
| width, height = size | ||
| errors = [[0] * width for _ in range(3)] | ||
| output: list[int] = [] | ||
|
|
||
| for y in range(height): | ||
| for x in range(width): | ||
| weighted_error = errors[y % 3][x] | ||
| correction = abs(weighted_error) // 48 | ||
| if weighted_error < 0: | ||
| correction = -correction | ||
| value = max(0, min(255, values[y * width + x] + correction)) | ||
| color = 255 if value > 128 else 0 | ||
| output.append(color) | ||
|
|
||
| for dx, dy, weight in TAPS: | ||
| xx, yy = x + dx, y + dy | ||
| if 0 <= xx < width and yy < height: | ||
| errors[yy % 3][xx] += (value - color) * weight | ||
|
|
||
| errors[y % 3] = [0] * width | ||
|
|
||
| return output | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("size", [(1, 1), (1, 9), (9, 1), (3, 3), (13, 11)]) | ||
| @pytest.mark.parametrize("mode", ["L", "RGB"]) | ||
| def test_jjn_bilevel_matches_diffusion_kernel(mode: str, size: tuple[int, int]) -> None: | ||
| width, height = size | ||
| values = [(x * 37 + y * 29 + 65) % 256 for y in range(height) for x in range(width)] | ||
| if mode == "RGB": | ||
| im = Image.new("RGB", size) | ||
| im.putdata([(v, v, v) for v in values]) | ||
| else: | ||
| im = Image.new("L", size) | ||
| im.putdata(values) | ||
|
|
||
| result = im.convert("1", dither=Image.Dither.JARVIS_JUDICE_NINKE) | ||
|
|
||
| assert result.mode == "1" | ||
| assert list(result.getdata()) == _expected_bilevel(values, size) | ||
|
|
||
|
|
||
| def test_jjn_palette_differs_from_floyd_steinberg() -> None: | ||
| size = (33, 15) | ||
| im = Image.new("RGB", size) | ||
| pixels = [ | ||
| ((x * 7 + y * 11) % 256,) * 3 for y in range(size[1]) for x in range(size[0]) | ||
| ] | ||
| im.putdata(pixels) | ||
| palette = Image.new("P", (1, 1)) | ||
| palette.putpalette((0, 0, 0, 255, 255, 255)) | ||
|
|
||
| jjn = im.quantize(palette=palette, dither=Image.Dither.JARVIS_JUDICE_NINKE) | ||
| floyd = im.quantize(palette=palette, dither=Image.Dither.FLOYDSTEINBERG) | ||
|
|
||
| assert jjn.mode == "P" | ||
| assert set(jjn.getdata()) <= {0, 1} | ||
| assert jjn.tobytes() != floyd.tobytes() | ||
|
|
||
|
|
||
| def test_jjn_hopper_reference_image() -> None: | ||
| # Compare with a checked-in reference image, not the Python diffusion model. | ||
| source = hopper("RGB").crop((24, 24, 56, 56)) | ||
| result = source.convert("1", dither=Image.Dither.JARVIS_JUDICE_NINKE) | ||
| with Image.open("Tests/images/hopper_jjn.pbm") as expected: | ||
| assert_image_equal(result, expected) | ||
|
|
||
|
|
||
| def test_jjn_empty_image() -> None: | ||
| assert Image.new("L", (0, 0)).convert( | ||
| "1", dither=Image.Dither.JARVIS_JUDICE_NINKE | ||
| ).size == (0, 0) | ||
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
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.
Is this just a reimplementation of JJN in Python? I think house style would be to add a JJN-dithered Hopper test fixture image to compare against?