forked from mtusi2026/mtusi2026lab5
RabbitMQ + transactional outbox, отдельный worker (thumbnail/crop/WebP), lease и heartbeat, retry с backoff+jitter, DLQ и replay, admission control; тесты отказов, трек 1 и отчёт.
116 lines
4.5 KiB
Python
116 lines
4.5 KiB
Python
"""Variant generation, run by the worker as a CHILD PROCESS with rlimits (address space, CPU seconds) and a
|
|
wall-clock timeout, so a hostile or huge image can only kill this process, never the worker.
|
|
|
|
usage: python -m app.imageproc <source> <out_dir> <max_pixels>
|
|
stdout: JSON list of produced files. Exit codes: 0 ok, 3 invalid_image, 4 image_too_large, 5 image_too_complex.
|
|
|
|
Rules (contracts/README.md "Фоновая обработка"):
|
|
thumbnail JPEG, fit into 256x256 keeping aspect ratio, never upscale, floor rounding, min 1 px
|
|
crop JPEG, centred square scaled to 128x128 (small sources are upscaled)
|
|
optimized WebP, fit into 1280x1280, never upscale, quality WEBP_QUALITY, method 4
|
|
EXIF orientation is applied first; alpha is composited on white for JPEG (WebP keeps alpha).
|
|
EXIF/XMP/ICC are not copied to any variant (pixels are treated as sRGB).
|
|
"""
|
|
import json
|
|
import math
|
|
import os
|
|
import sys
|
|
|
|
from PIL import Image, ImageFile, ImageOps
|
|
|
|
Image.MAX_IMAGE_PIXELS = None # we enforce our own limit before decoding
|
|
ImageFile.LOAD_TRUNCATED_IMAGES = False
|
|
|
|
THUMB, CROP, OPTIMIZED = 256, 128, 1280
|
|
JPEG_QUALITY = int(os.environ.get("JPEG_QUALITY", "85"))
|
|
WEBP_QUALITY = int(os.environ.get("WEBP_QUALITY", "80"))
|
|
ORIENTATION_TAG = 0x0112
|
|
EXIT_INVALID, EXIT_TOO_LARGE, EXIT_TOO_COMPLEX = 3, 4, 5
|
|
|
|
|
|
def fit(width: int, height: int, bound: int):
|
|
"""Largest size inside bound x bound with the same aspect ratio; floor rounding; never upscales."""
|
|
if width <= bound and height <= bound:
|
|
return width, height
|
|
if width >= height:
|
|
return bound, max(1, height * bound // width)
|
|
return max(1, width * bound // height), bound
|
|
|
|
|
|
def has_alpha(img) -> bool:
|
|
return img.mode in ("RGBA", "LA", "PA") or (img.mode == "P" and "transparency" in img.info)
|
|
|
|
|
|
def to_8bit(img):
|
|
if img.mode in ("I;16", "I;16B", "I;16L", "I"):
|
|
return img.convert("I").point(lambda v: v * (1 / 257)).convert("L")
|
|
return img
|
|
|
|
|
|
def on_white(img):
|
|
if has_alpha(img):
|
|
rgba = img.convert("RGBA")
|
|
flat = Image.new("RGB", rgba.size, (255, 255, 255))
|
|
flat.paste(rgba, mask=rgba.getchannel("A"))
|
|
return flat, rgba
|
|
return img.convert("RGB"), None
|
|
|
|
|
|
def process(source: str, out_dir: str, max_pixels: int) -> list:
|
|
img = Image.open(source)
|
|
if img.format not in ("PNG", "JPEG"):
|
|
raise ValueError("format")
|
|
width, height = img.size
|
|
if width * height > max_pixels:
|
|
sys.exit(EXIT_TOO_LARGE)
|
|
orientation = img.getexif().get(ORIENTATION_TAG, 1)
|
|
upright = (height, width) if orientation in (5, 6, 7, 8) else (width, height)
|
|
thumb_size = fit(*upright, THUMB)
|
|
opt_size = fit(*upright, OPTIMIZED)
|
|
if img.format == "JPEG":
|
|
# DCT scaling: decode directly at >= the largest size we need (memory and time, exact output sizes kept).
|
|
need = opt_size if orientation not in (5, 6, 7, 8) else (opt_size[1], opt_size[0])
|
|
img.draft(img.mode, (math.ceil(need[0]), math.ceil(need[1])))
|
|
img.load()
|
|
img = ImageOps.exif_transpose(img)
|
|
img = to_8bit(img)
|
|
flat, rgba = on_white(img)
|
|
del img
|
|
|
|
results = []
|
|
|
|
def save(name, image, ext, fmt, content_type, **params):
|
|
path = os.path.join(out_dir, name + "." + ext)
|
|
image.save(path, fmt, **params)
|
|
results.append({"name": name, "file": path, "content_type": content_type,
|
|
"width": image.size[0], "height": image.size[1]})
|
|
|
|
jpeg = {"quality": JPEG_QUALITY, "optimize": True, "progressive": False}
|
|
save("thumbnail", flat.resize(thumb_size, Image.Resampling.LANCZOS, reducing_gap=3.0),
|
|
"jpg", "JPEG", "image/jpeg", **jpeg)
|
|
w, h = flat.size
|
|
side = min(w, h)
|
|
left, top = (w - side) // 2, (h - side) // 2
|
|
save("crop", flat.resize((CROP, CROP), Image.Resampling.LANCZOS, box=(left, top, left + side, top + side)),
|
|
"jpg", "JPEG", "image/jpeg", **jpeg)
|
|
source_for_webp = rgba if rgba is not None else flat
|
|
optimized = source_for_webp if source_for_webp.size == opt_size else source_for_webp.resize(
|
|
opt_size, Image.Resampling.LANCZOS, reducing_gap=3.0)
|
|
save("optimized", optimized, "webp", "WEBP", "image/webp", quality=WEBP_QUALITY, method=4)
|
|
return results
|
|
|
|
|
|
def main() -> None:
|
|
source, out_dir, max_pixels = sys.argv[1], sys.argv[2], int(sys.argv[3])
|
|
try:
|
|
results = process(source, out_dir, max_pixels)
|
|
except MemoryError:
|
|
sys.exit(EXIT_TOO_COMPLEX)
|
|
except (OSError, ValueError, SyntaxError, Image.DecompressionBombError):
|
|
sys.exit(EXIT_INVALID)
|
|
json.dump(results, sys.stdout)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|