forked from mtusi2026/mtusi2026lab5
all for 4
This commit is contained in:
@@ -0,0 +1,176 @@
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import resource
|
||||
import sys
|
||||
import time
|
||||
import warnings
|
||||
|
||||
from PIL import Image, ImageFile, ImageOps, UnidentifiedImageError
|
||||
|
||||
|
||||
MAX_BYTES = 10 * 1024 * 1024
|
||||
MAX_PIXELS = 40_000_000
|
||||
|
||||
|
||||
def fit_size(width, height, bound):
|
||||
denominator = max(bound, width, height)
|
||||
|
||||
return (
|
||||
max(1, width * bound // denominator),
|
||||
max(1, height * bound // denominator),
|
||||
)
|
||||
|
||||
|
||||
def white_rgb(image):
|
||||
rgba = image.convert("RGBA")
|
||||
output = Image.new("RGB", rgba.size, "white")
|
||||
|
||||
output.paste(
|
||||
rgba,
|
||||
mask=rgba.getchannel("A"),
|
||||
)
|
||||
|
||||
rgba.close()
|
||||
return output
|
||||
|
||||
|
||||
def main():
|
||||
# Ограничения дочернего процесса Linux.
|
||||
resource.setrlimit(
|
||||
resource.RLIMIT_AS,
|
||||
(768 * 1024**2,) * 2,
|
||||
)
|
||||
|
||||
resource.setrlimit(
|
||||
resource.RLIMIT_CPU,
|
||||
(20, 21),
|
||||
)
|
||||
|
||||
resource.setrlimit(
|
||||
resource.RLIMIT_FSIZE,
|
||||
(11 * 1024**2,) * 2,
|
||||
)
|
||||
|
||||
Image.MAX_IMAGE_PIXELS = MAX_PIXELS
|
||||
ImageFile.LOAD_TRUNCATED_IMAGES = False
|
||||
|
||||
warnings.simplefilter(
|
||||
"error",
|
||||
Image.DecompressionBombWarning,
|
||||
)
|
||||
|
||||
source, output = map(Path, sys.argv[1:3])
|
||||
|
||||
if not 0 < source.stat().st_size <= MAX_BYTES:
|
||||
return 10
|
||||
|
||||
if os.environ.get("IMAGE_TEST_SLEEP") == "1":
|
||||
time.sleep(60)
|
||||
|
||||
try:
|
||||
image = Image.open(source)
|
||||
|
||||
if (
|
||||
image.format not in ("PNG", "JPEG")
|
||||
or image.width * image.height > MAX_PIXELS
|
||||
):
|
||||
image.close()
|
||||
return 10
|
||||
|
||||
image.load()
|
||||
ImageOps.exif_transpose(image, in_place=True)
|
||||
|
||||
except (
|
||||
ValueError,
|
||||
UnidentifiedImageError,
|
||||
OSError,
|
||||
SyntaxError,
|
||||
Image.DecompressionBombWarning,
|
||||
Image.DecompressionBombError,
|
||||
):
|
||||
return 10
|
||||
|
||||
with image:
|
||||
manifest = []
|
||||
|
||||
variants = [
|
||||
("thumbnail", 256, "image/jpeg", "jpg"),
|
||||
("crop", 128, "image/jpeg", "jpg"),
|
||||
("optimized", 1280, "image/webp", "webp"),
|
||||
]
|
||||
|
||||
for name, bound, mime, extension in variants:
|
||||
if name == "crop":
|
||||
scaled = ImageOps.fit(
|
||||
image,
|
||||
(128, 128),
|
||||
method=Image.Resampling.LANCZOS,
|
||||
centering=(0.5, 0.5),
|
||||
)
|
||||
else:
|
||||
scaled = image.resize(
|
||||
fit_size(*image.size, bound),
|
||||
Image.Resampling.LANCZOS,
|
||||
)
|
||||
|
||||
if extension == "jpg":
|
||||
rendered = white_rgb(scaled)
|
||||
else:
|
||||
rendered = scaled.convert("RGBA")
|
||||
|
||||
scaled.close()
|
||||
|
||||
rendered.info.clear()
|
||||
|
||||
path = output / (name + "." + extension)
|
||||
|
||||
if extension == "jpg":
|
||||
rendered.save(
|
||||
path,
|
||||
"JPEG",
|
||||
quality=85,
|
||||
subsampling=2,
|
||||
optimize=True,
|
||||
exif=b"",
|
||||
icc_profile=None,
|
||||
)
|
||||
else:
|
||||
rendered.save(
|
||||
path,
|
||||
"WEBP",
|
||||
quality=80,
|
||||
method=4,
|
||||
lossless=False,
|
||||
exif=b"",
|
||||
icc_profile=b"",
|
||||
xmp=b"",
|
||||
)
|
||||
|
||||
manifest.append(
|
||||
{
|
||||
"name": name,
|
||||
"content_type": mime,
|
||||
"width": rendered.width,
|
||||
"height": rendered.height,
|
||||
}
|
||||
)
|
||||
|
||||
rendered.close()
|
||||
|
||||
if not 0 < path.stat().st_size <= MAX_BYTES:
|
||||
return 11
|
||||
|
||||
(output / "manifest.json").write_text(
|
||||
json.dumps(manifest),
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
sys.exit(main())
|
||||
except (MemoryError, OSError):
|
||||
sys.exit(11)
|
||||
Reference in New Issue
Block a user