ЛР 6 — БВТ2304 — Шемрук — на 5 #121
+3
-3
@@ -116,12 +116,12 @@ def claim(store, message):
|
||||
return job
|
||||
|
||||
|
||||
def encode_images(original):
|
||||
def encode_images(original, version):
|
||||
with tempfile.TemporaryDirectory(prefix="messenger-image-") as name:
|
||||
root = Path(name)
|
||||
(root / "source").write_bytes(original)
|
||||
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,
|
||||
)
|
||||
if result.returncode == 2:
|
||||
@@ -136,7 +136,7 @@ def try_publish(store, objects, job):
|
||||
attachment_id, version = job["attachment_id"], job["pipeline_version"]
|
||||
with tm.span("s3.staging.get", tm.SpanKind.CLIENT):
|
||||
original = objects.get(objects.key(attachment_id, "staging"))
|
||||
processed = encode_images(original)
|
||||
processed = encode_images(original, version)
|
||||
with store.pool.connection() as connection:
|
||||
current = connection.execute("""
|
||||
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)
|
||||
|
||||
|
||||
def convert(root):
|
||||
def convert(root, version):
|
||||
try:
|
||||
with Image.open(root / "source") as source:
|
||||
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"))
|
||||
except (UnidentifiedImageError, Image.DecompressionBombError, SyntaxError, OSError):
|
||||
return 2
|
||||
jpeg_quality, webp_quality = (85, 82) if version == 1 else (89, 84)
|
||||
outputs = [
|
||||
("thumbnail", fit(rgb, 256), "JPEG", "image/jpeg", 85),
|
||||
("crop", ImageOps.fit(rgb, (128, 128), Image.Resampling.LANCZOS), "JPEG", "image/jpeg", 85),
|
||||
("optimized", fit(rgb, 1280), "WEBP", "image/webp", 82),
|
||||
("thumbnail", fit(rgb, 256), "JPEG", "image/jpeg", jpeg_quality),
|
||||
("crop", ImageOps.fit(rgb, (128, 128), Image.Resampling.LANCZOS), "JPEG", "image/jpeg", jpeg_quality),
|
||||
("optimized", fit(rgb, 1280), "WEBP", "image/webp", webp_quality),
|
||||
]
|
||||
descriptors = []
|
||||
for name, image, format_name, mime, quality in outputs:
|
||||
@@ -42,4 +43,4 @@ def convert(root):
|
||||
|
||||
|
||||
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