forked from mtusi2026/mtusi2026lab5
52 lines
2.0 KiB
Python
52 lines
2.0 KiB
Python
"""Run deterministic image transforms without a server or object store."""
|
|
from __future__ import annotations
|
|
|
|
from hashlib import sha256
|
|
from io import BytesIO
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
from PIL import Image
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT))
|
|
from src.app.image_pipeline import process_image
|
|
|
|
|
|
def main() -> None:
|
|
source_image = Image.new("RGB", (640, 480), (32, 128, 210))
|
|
exif = Image.Exif()
|
|
exif[0x010E] = "synthetic test metadata"
|
|
source = BytesIO()
|
|
source_image.save(source, format="JPEG", quality=90, exif=exif)
|
|
source_image.close()
|
|
results = process_image(source.getvalue(), "image/jpeg", 40_000_000, "synthetic")
|
|
observed = {}
|
|
for item in results:
|
|
with Image.open(BytesIO(item["body"])) as image:
|
|
dimensions = image.size
|
|
has_exif = bool(image.getexif())
|
|
if has_exif or dimensions != (item["width"], item["height"]):
|
|
raise SystemExit("FAIL: variant metadata or dimensions do not match")
|
|
if sha256(item["body"]).hexdigest() != item["sha256"]:
|
|
raise SystemExit("FAIL: variant checksum mismatch")
|
|
observed[item["name"]] = {
|
|
"content_type": item["content_type"],
|
|
"dimensions": list(dimensions),
|
|
"exif_removed": True,
|
|
"sha256_valid": True,
|
|
}
|
|
expected = {
|
|
"thumbnail": {"content_type": "image/jpeg", "dimensions": [256, 192], "exif_removed": True, "sha256_valid": True},
|
|
"crop": {"content_type": "image/jpeg", "dimensions": [128, 128], "exif_removed": True, "sha256_valid": True},
|
|
"optimized": {"content_type": "image/webp", "dimensions": [640, 480], "exif_removed": True, "sha256_valid": True},
|
|
}
|
|
if observed != expected:
|
|
raise SystemExit("FAIL: generated image variants do not meet the grade-4 contract")
|
|
print(json.dumps({"result": "PASS", "variants": observed}, separators=(",", ":")))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|