-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
feat(preprod): Add image comparison library with odiff batch support #109381
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
62bb5cc
feat(preprod): Add image comparison library with odiff batch support
NicoHinderling 83afc5a
bot callout
NicoHinderling 37d7362
add explicit image handling
NicoHinderling 618320e
sentry bot comment
NicoHinderling e3a5d47
fix(preprod): Prevent image resource leak on corrupt input
NicoHinderling d1b4a99
hopefully better
NicoHinderling 6f57c5b
ref(preprod): Address image diff review feedback — CI odiff setup, er…
NicoHinderling 887238a
more rbro requests
NicoHinderling e6b1e0a
address CI
NicoHinderling 1bbe926
simplify
NicoHinderling 2d825ec
this too
NicoHinderling 69b75db
more bot feedback
NicoHinderling 3a9e4d4
final feedback
NicoHinderling 5d1761a
make threshold 0
NicoHinderling 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import base64 | ||
| import io | ||
| import logging | ||
| import tempfile | ||
| from collections.abc import Sequence | ||
| from pathlib import Path | ||
|
|
||
| from PIL import Image | ||
|
|
||
| from .odiff import OdiffServer | ||
| from .types import DiffResult | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| DIFF_THRESHOLD = 0 | ||
|
|
||
|
|
||
| def _as_image(source: bytes | Image.Image) -> Image.Image: | ||
| if isinstance(source, bytes): | ||
| img = Image.open(io.BytesIO(source)) | ||
| try: | ||
| img.load() | ||
| except Exception: | ||
| img.close() | ||
| raise | ||
| return img | ||
| return source | ||
|
|
||
|
|
||
| def _mask_from_diff_output(output_path: Path) -> Image.Image: | ||
| with Image.open(output_path) as img: | ||
| rgba = img.convert("RGBA") | ||
| bands: tuple[Image.Image, ...] = () | ||
| try: | ||
| bands = rgba.split() | ||
| alpha = bands[3] | ||
| mask = alpha.point(lambda px: 255 if px > 0 else 0) | ||
| return mask | ||
| finally: | ||
| for band in bands: | ||
| band.close() | ||
| rgba.close() | ||
NicoHinderling marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def _encode_mask_png_base64(mask: Image.Image) -> str: | ||
| buf = io.BytesIO() | ||
| mask.save(buf, format="PNG") | ||
| return base64.b64encode(buf.getvalue()).decode("ascii") | ||
|
|
||
|
|
||
| def compare_images( | ||
| before: bytes | Image.Image, | ||
| after: bytes | Image.Image, | ||
| ) -> DiffResult | None: | ||
| return compare_images_batch([(before, after)])[0] | ||
NicoHinderling marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def compare_images_batch( | ||
| pairs: Sequence[tuple[bytes | Image.Image, bytes | Image.Image]], | ||
| server: OdiffServer | None = None, | ||
| ) -> list[DiffResult | None]: | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| tmpdir_path = Path(tmpdir) | ||
| if server is not None: | ||
| return _compare_pairs(pairs, server, tmpdir_path) | ||
| with OdiffServer() as new_server: | ||
| return _compare_pairs(pairs, new_server, tmpdir_path) | ||
|
|
||
|
|
||
| def _compare_pairs( | ||
| pairs: Sequence[tuple[bytes | Image.Image, bytes | Image.Image]], | ||
| server: OdiffServer, | ||
| tmpdir_path: Path, | ||
| ) -> list[DiffResult | None]: | ||
| return [ | ||
| _compare_single_pair(idx, before, after, server, tmpdir_path) | ||
| for idx, (before, after) in enumerate(pairs) | ||
| ] | ||
|
|
||
|
|
||
| def _compare_single_pair( | ||
| idx: int, | ||
| before: bytes | Image.Image, | ||
| after: bytes | Image.Image, | ||
| server: OdiffServer, | ||
| tmpdir_path: Path, | ||
| ) -> DiffResult | None: | ||
| before_img: Image.Image | None = None | ||
| after_img: Image.Image | None = None | ||
| diff_mask: Image.Image | None = None | ||
| try: | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| before_img = _as_image(before) | ||
| after_img = _as_image(after) | ||
| bw, bh = before_img.size | ||
| aw, ah = after_img.size | ||
| max_w = max(bw, aw) | ||
| max_h = max(bh, ah) | ||
|
|
||
| before_path = tmpdir_path / f"before_{idx}.png" | ||
| after_path = tmpdir_path / f"after_{idx}.png" | ||
| before_img.save(before_path, "PNG") | ||
| after_img.save(after_path, "PNG") | ||
|
|
||
| output_path = tmpdir_path / f"diff_{idx}.png" | ||
| resp = server.compare( | ||
| before_path, | ||
| after_path, | ||
| output_path, | ||
| threshold=DIFF_THRESHOLD, | ||
| antialiasing=True, | ||
| outputDiffMask=True, | ||
| failOnLayoutDiff=False, | ||
| ) | ||
| changed_pixels = resp.diffCount or 0 | ||
| diff_pct = resp.diffPercentage or 0.0 | ||
|
|
||
| total_pixels = max_w * max_h | ||
| diff_score = diff_pct / 100.0 | ||
|
|
||
| if changed_pixels == 0: | ||
| diff_mask = Image.new("L", (max_w, max_h), 0) | ||
| elif not output_path.exists(): | ||
| raise RuntimeError(f"odiff did not produce output file: {output_path}") | ||
| else: | ||
| diff_mask = _mask_from_diff_output(output_path) | ||
| if diff_mask.size != (max_w, max_h): | ||
| old_mask = diff_mask | ||
| diff_mask = diff_mask.resize((max_w, max_h), Image.NEAREST) | ||
| old_mask.close() | ||
|
|
||
| diff_mask_png = _encode_mask_png_base64(diff_mask) | ||
|
|
||
| return DiffResult( | ||
| diff_mask_png=diff_mask_png, | ||
| diff_score=diff_score, | ||
| changed_pixels=changed_pixels, | ||
| total_pixels=total_pixels, | ||
| aligned_height=max_h, | ||
| width=max_w, | ||
| before_width=bw, | ||
| before_height=bh, | ||
| after_width=aw, | ||
| after_height=ah, | ||
| ) | ||
| except Exception: | ||
| logger.exception("Failed to compare image pair %d", idx) | ||
| return None | ||
| finally: | ||
| if before_img is not None and isinstance(before, bytes): | ||
| before_img.close() | ||
| if after_img is not None and isinstance(after, bytes): | ||
| after_img.close() | ||
| if diff_mask is not None: | ||
| diff_mask.close() | ||
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
83 changes: 83 additions & 0 deletions
83
tests/sentry/preprod/snapshots/image_diff/test_image_diff.py
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,83 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import io | ||
|
|
||
| from PIL import Image, ImageDraw | ||
|
|
||
| from sentry.preprod.snapshots.image_diff.compare import compare_images, compare_images_batch | ||
|
|
||
|
|
||
| def _make_solid_image(width: int, height: int, color: tuple[int, int, int, int]) -> Image.Image: | ||
| return Image.new("RGBA", (width, height), color) | ||
|
|
||
|
|
||
| class TestCompareImages: | ||
| def test_identical_images(self): | ||
| img = _make_solid_image(100, 100, (128, 128, 128, 255)) | ||
| result = compare_images(img, img.copy()) | ||
| assert result is not None | ||
| assert result.diff_score == 0.0 | ||
| assert result.changed_pixels == 0 | ||
| assert result.total_pixels == 100 * 100 | ||
|
|
||
| def test_different_sizes(self): | ||
| small = _make_solid_image(30, 30, (100, 100, 100, 255)) | ||
| large = _make_solid_image(50, 50, (100, 100, 100, 255)) | ||
| result = compare_images(small, large) | ||
| assert result is not None | ||
| assert result.width == 50 | ||
| assert result.aligned_height == 50 | ||
| assert result.before_width == 30 | ||
| assert result.before_height == 30 | ||
| assert result.after_width == 50 | ||
| assert result.after_height == 50 | ||
|
|
||
| def test_modified_block(self): | ||
| before = _make_solid_image(100, 100, (100, 100, 100, 255)) | ||
| after = _make_solid_image(100, 100, (100, 100, 100, 255)) | ||
| draw = ImageDraw.Draw(after) | ||
| draw.rectangle((10, 10, 29, 29), fill=(255, 0, 0, 255)) | ||
| result = compare_images(before, after) | ||
| assert result is not None | ||
| assert result.changed_pixels > 0 | ||
|
|
||
| def test_bytes_input(self): | ||
| img = _make_solid_image(30, 30, (128, 128, 128, 255)) | ||
| buf = io.BytesIO() | ||
| img.save(buf, format="PNG") | ||
| img_bytes = buf.getvalue() | ||
|
|
||
| result = compare_images(img_bytes, img_bytes) | ||
| assert result is not None | ||
| assert result.diff_score == 0.0 | ||
|
|
||
|
|
||
| class TestCompareImagesBatch: | ||
| def test_batch_returns_correct_count(self): | ||
| img1 = _make_solid_image(50, 50, (100, 100, 100, 255)) | ||
| img2 = _make_solid_image(50, 50, (200, 200, 200, 255)) | ||
|
|
||
| results = compare_images_batch( | ||
| [ | ||
| (img1, img1.copy()), | ||
| (img1, img2), | ||
| ] | ||
| ) | ||
|
|
||
| assert len(results) == 2 | ||
| assert results[0] is not None | ||
| assert results[1] is not None | ||
| assert results[0].diff_score == 0.0 | ||
| assert results[1].diff_score > 0.0 | ||
|
|
||
| def test_batch_single_pair_matches_single(self): | ||
| before = _make_solid_image(50, 50, (100, 100, 100, 255)) | ||
| after = _make_solid_image(50, 50, (200, 200, 200, 255)) | ||
|
|
||
| single = compare_images(before, after) | ||
| batch = compare_images_batch([(before, after)])[0] | ||
|
|
||
| assert single is not None | ||
| assert batch is not None | ||
| assert single.diff_score == batch.diff_score | ||
| assert single.changed_pixels == batch.changed_pixels |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.