fix: preserve PNG transparency at original sample depth
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
PYTHONPATH=src:student-tests .venv/bin/python - <<'PY'
|
||||
import hashlib
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
import types
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
from PIL import Image, __version__
|
||||
from messenger.attachments.infrastructure import images
|
||||
from pictures import SAMPLE, chunk
|
||||
from png_trns_fixtures import png_with_trns
|
||||
|
||||
baseline = "6e97bdbe9638c5a53951337fce639a1a0db7f92e"
|
||||
name = "messenger.attachments.infrastructure.before_trns_depth"
|
||||
old = types.ModuleType(name)
|
||||
sys.modules[name] = old
|
||||
source = subprocess.run(
|
||||
["git", "show", baseline + ":src/messenger/attachments/infrastructure/images.py"],
|
||||
check=True, capture_output=True, text=True,
|
||||
).stdout
|
||||
exec(compile(source, name, "exec"), old.__dict__)
|
||||
|
||||
def encoded(image, kind="PNG", **options):
|
||||
buffer = BytesIO()
|
||||
image.save(buffer, kind, **options)
|
||||
return buffer.getvalue()
|
||||
|
||||
gradient = Image.linear_gradient("L").resize((320, 200))
|
||||
fixtures = {"sample": SAMPLE}
|
||||
for mode in ("RGB", "CMYK"):
|
||||
fixtures[mode + "-jpeg"] = encoded(gradient.convert(mode), "JPEG")
|
||||
for mode in ("RGB", "L", "I;16", "P", "RGBA", "LA", "1"):
|
||||
fixtures[mode + "-png"] = encoded(gradient.convert(mode))
|
||||
fixtures["P-trns"] = encoded(gradient.convert("P"), transparency=0)
|
||||
fixtures["P-alpha-table"] = encoded(gradient.convert("P"), transparency=bytes([0, 128, 255]))
|
||||
for mode, key in [("RGB", (32, 96, 160)), ("L", 85), ("I;16", 12345), ("1", 0)]:
|
||||
fixtures[mode + "-trns"] = encoded(Image.new(mode, (64, 64), key), transparency=key)
|
||||
for depth, key in [(2, (1,)), (4, (5,)), (16, (12345, 23456, 34567))]:
|
||||
data, _ = png_with_trns(depth, key)
|
||||
trns = chunk(b"tRNS", b"".join(value.to_bytes(2, "big") for value in key))
|
||||
fixtures[str(depth) + "-no-trns-" + str(len(key))] = data.replace(trns, b"")
|
||||
|
||||
limits = images.RenderLimits(10_485_760, 40_000_000)
|
||||
rows = []
|
||||
for name, data in fixtures.items():
|
||||
for version in (1, 2):
|
||||
before = old.render_variants(data, limits, version)
|
||||
after = images.render_variants(data, limits, version)
|
||||
assert before == after, (name, version)
|
||||
rows.append({
|
||||
"fixture": name, "source_sha256": hashlib.sha256(data).hexdigest(),
|
||||
"version": version,
|
||||
"variants": {v.name: hashlib.sha256(v.data).hexdigest() for v in after},
|
||||
})
|
||||
report = {
|
||||
"baseline_sha": baseline, "pillow": __version__, "fixtures": len(fixtures),
|
||||
"identical_variants": len(rows) * 3, "results": rows,
|
||||
}
|
||||
Path("evidence/review-trns-depth-2026-11-06/unchanged-rendering.json").write_text(
|
||||
json.dumps(report, indent=2) + "\n"
|
||||
)
|
||||
print(json.dumps({key: value for key, value in report.items() if key != "results"}))
|
||||
PY
|
||||
Reference in New Issue
Block a user