forked from mtusi2026/mtusi2026lab6
Уточнить проверку и доказательства ЛР 6
This commit is contained in:
+3
-3
@@ -116,12 +116,12 @@ def claim(store, message):
|
|||||||
return job
|
return job
|
||||||
|
|
||||||
|
|
||||||
def encode_images(original):
|
def encode_images(original, version):
|
||||||
with tempfile.TemporaryDirectory(prefix="messenger-image-") as name:
|
with tempfile.TemporaryDirectory(prefix="messenger-image-") as name:
|
||||||
root = Path(name)
|
root = Path(name)
|
||||||
(root / "source").write_bytes(original)
|
(root / "source").write_bytes(original)
|
||||||
result = subprocess.run(
|
result = subprocess.run(
|
||||||
[sys.executable, "-m", "variants", str(root)], check=False,
|
[sys.executable, "-m", "variants", str(root), str(version)], check=False,
|
||||||
timeout=CHILD_TIMEOUT, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
timeout=CHILD_TIMEOUT, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
||||||
)
|
)
|
||||||
if result.returncode == 2:
|
if result.returncode == 2:
|
||||||
@@ -136,7 +136,7 @@ def try_publish(store, objects, job):
|
|||||||
attachment_id, version = job["attachment_id"], job["pipeline_version"]
|
attachment_id, version = job["attachment_id"], job["pipeline_version"]
|
||||||
with tm.span("s3.staging.get", tm.SpanKind.CLIENT):
|
with tm.span("s3.staging.get", tm.SpanKind.CLIENT):
|
||||||
original = objects.get(objects.key(attachment_id, "staging"))
|
original = objects.get(objects.key(attachment_id, "staging"))
|
||||||
processed = encode_images(original)
|
processed = encode_images(original, version)
|
||||||
with store.pool.connection() as connection:
|
with store.pool.connection() as connection:
|
||||||
current = connection.execute("""
|
current = connection.execute("""
|
||||||
SELECT j.attempt, j.state, a.pipeline_version,
|
SELECT j.attempt, j.state, a.pipeline_version,
|
||||||
|
|||||||
+6
-5
@@ -17,7 +17,7 @@ def fit(image, limit):
|
|||||||
return image.resize(size, Image.Resampling.LANCZOS)
|
return image.resize(size, Image.Resampling.LANCZOS)
|
||||||
|
|
||||||
|
|
||||||
def convert(root):
|
def convert(root, version):
|
||||||
try:
|
try:
|
||||||
with Image.open(root / "source") as source:
|
with Image.open(root / "source") as source:
|
||||||
if source.width * source.height > MAX_PIXELS or source.format not in ("PNG", "JPEG"):
|
if source.width * source.height > MAX_PIXELS or source.format not in ("PNG", "JPEG"):
|
||||||
@@ -27,10 +27,11 @@ def convert(root):
|
|||||||
rgb.paste(image, mask=image.getchannel("A"))
|
rgb.paste(image, mask=image.getchannel("A"))
|
||||||
except (UnidentifiedImageError, Image.DecompressionBombError, SyntaxError, OSError):
|
except (UnidentifiedImageError, Image.DecompressionBombError, SyntaxError, OSError):
|
||||||
return 2
|
return 2
|
||||||
|
jpeg_quality, webp_quality = (85, 82) if version == 1 else (89, 84)
|
||||||
outputs = [
|
outputs = [
|
||||||
("thumbnail", fit(rgb, 256), "JPEG", "image/jpeg", 85),
|
("thumbnail", fit(rgb, 256), "JPEG", "image/jpeg", jpeg_quality),
|
||||||
("crop", ImageOps.fit(rgb, (128, 128), Image.Resampling.LANCZOS), "JPEG", "image/jpeg", 85),
|
("crop", ImageOps.fit(rgb, (128, 128), Image.Resampling.LANCZOS), "JPEG", "image/jpeg", jpeg_quality),
|
||||||
("optimized", fit(rgb, 1280), "WEBP", "image/webp", 82),
|
("optimized", fit(rgb, 1280), "WEBP", "image/webp", webp_quality),
|
||||||
]
|
]
|
||||||
descriptors = []
|
descriptors = []
|
||||||
for name, image, format_name, mime, quality in outputs:
|
for name, image, format_name, mime, quality in outputs:
|
||||||
@@ -42,4 +43,4 @@ def convert(root):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
sys.exit(convert(Path(sys.argv[1])))
|
sys.exit(convert(Path(sys.argv[1]), int(sys.argv[2])))
|
||||||
|
|||||||
Reference in New Issue
Block a user