117 lines
4.1 KiB
Python
117 lines
4.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Контракт изображений: EXIF orientation, alpha→white, метаданные удалены."""
|
|
from __future__ import annotations
|
|
|
|
import io
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from PIL import Image
|
|
|
|
HERE = Path(__file__).resolve().parent
|
|
ROOT = HERE.parent
|
|
for candidate in (str(HERE), str(ROOT / "tests")):
|
|
if candidate not in sys.path:
|
|
sys.path.insert(0, candidate)
|
|
|
|
from client import Client, arguments # noqa: E402
|
|
|
|
|
|
def png_rgba() -> bytes:
|
|
image = Image.new("RGBA", (100, 50), (255, 0, 0, 255))
|
|
for x in range(50, 100):
|
|
for y in range(50):
|
|
image.putpixel((x, y), (0, 0, 255, 0))
|
|
buffer = io.BytesIO()
|
|
image.save(buffer, format="PNG")
|
|
return buffer.getvalue()
|
|
|
|
|
|
def jpeg_with_exif() -> bytes:
|
|
image = Image.new("RGB", (320, 200), (10, 120, 220))
|
|
exif = Image.Exif()
|
|
exif[274] = 6 # Orientation: rotate 90 CW
|
|
buffer = io.BytesIO()
|
|
image.save(buffer, format="JPEG", quality=90, exif=exif)
|
|
return buffer.getvalue()
|
|
|
|
|
|
def upload(api: Client, actor: dict, chat: dict, content: bytes, filename: str, content_type: str) -> dict:
|
|
from urllib.parse import quote
|
|
|
|
return api.call(
|
|
"POST",
|
|
"/api/v1/chats/%s/attachments?filename=%s" % (chat["id"], quote(filename, safe="")),
|
|
202,
|
|
actor=actor,
|
|
raw=content,
|
|
headers={"Content-Type": content_type},
|
|
schema="Attachment",
|
|
)["json"]
|
|
|
|
|
|
def download(api: Client, actor: dict, attachment_id: str, variant: str) -> bytes:
|
|
return api.call(
|
|
"GET",
|
|
"/api/v1/attachments/" + attachment_id + "/content?variant=" + variant,
|
|
actor=actor,
|
|
)["bytes"]
|
|
|
|
|
|
def main() -> int:
|
|
args = arguments("Image contract probe")
|
|
api = Client(args.base_url, args.ca_file, 30)
|
|
alice, bob, eve, chat = api.bootstrap()
|
|
|
|
rgba = upload(api, alice, chat, png_rgba(), "alpha.png", "image/png")
|
|
rgba_ready = api.wait_ready(bob, rgba)
|
|
thumb = Image.open(io.BytesIO(download(api, bob, rgba["id"], "thumbnail"))).convert("RGB")
|
|
thumb_pixels = [thumb.getpixel((90, 10)), thumb.getpixel((90, 40))]
|
|
|
|
exif = upload(api, alice, chat, jpeg_with_exif(), "oriented.jpg", "image/jpeg")
|
|
exif_ready = api.wait_ready(bob, exif)
|
|
optimized = Image.open(io.BytesIO(download(api, bob, exif["id"], "optimized")))
|
|
thumbnail = Image.open(io.BytesIO(download(api, bob, exif["id"], "thumbnail")))
|
|
|
|
evidence = {
|
|
"scenario": "image_contract",
|
|
"alpha": {
|
|
"attachment_id": rgba["id"],
|
|
"status": rgba_ready["status"],
|
|
"thumbnail_size": thumb.size,
|
|
"alpha_region_rgb": thumb_pixels,
|
|
"alpha_composited_on_white": all(min(pixel) >= 245 for pixel in thumb_pixels),
|
|
},
|
|
"exif_orientation": {
|
|
"attachment_id": exif["id"],
|
|
"source_size": [320, 200],
|
|
"oriented_size": [200, 320],
|
|
"thumbnail_size": list(thumbnail.size),
|
|
"optimized_size": list(optimized.size),
|
|
"thumbnail_exif_entries": len(getattr(thumbnail, "getexif", lambda: {})()),
|
|
"optimized_exif_entries": len(getattr(optimized, "getexif", lambda: {})()),
|
|
"orientation_applied": thumbnail.size == (160, 256),
|
|
"metadata_removed": len(getattr(thumbnail, "getexif", lambda: {})()) == 0
|
|
and len(getattr(optimized, "getexif", lambda: {})()) == 0,
|
|
},
|
|
"variants": {
|
|
"thumbnail": "image/jpeg 160x256 for rotated 200x320",
|
|
"crop": "image/jpeg centered 128x128",
|
|
"optimized": "image/webp fit 1280x1280 without upscale",
|
|
},
|
|
}
|
|
evidence["passed"] = (
|
|
evidence["alpha"]["alpha_composited_on_white"]
|
|
and evidence["exif_orientation"]["orientation_applied"]
|
|
and evidence["exif_orientation"]["metadata_removed"]
|
|
)
|
|
output = ROOT / "evidence" / "image-contract.json"
|
|
output.write_text(json.dumps(evidence, ensure_ascii=False, indent=2, sort_keys=True), encoding="utf-8")
|
|
print(json.dumps(evidence, ensure_ascii=False, indent=2, sort_keys=True))
|
|
return 0 if evidence["passed"] else 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|