Реализовать наблюдаемость мессенджера и проверить её под нагрузкой
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
"""Pure, versioned image transforms. No network, filenames, or EXIF in output."""
|
||||
|
||||
import io
|
||||
|
||||
from PIL import Image, ImageOps
|
||||
|
||||
|
||||
def fit(image, maximum):
|
||||
scale = max(image.size)
|
||||
if scale <= maximum:
|
||||
return image.copy()
|
||||
size = tuple(max(1, n * maximum // scale) for n in image.size)
|
||||
return image.resize(size, Image.Resampling.LANCZOS)
|
||||
|
||||
|
||||
def variants(source, version):
|
||||
if version not in (1, 2):
|
||||
raise ValueError("Unsupported pipeline version")
|
||||
with Image.open(source) as raw:
|
||||
oriented = ImageOps.exif_transpose(raw)
|
||||
rgba = oriented.convert("RGBA")
|
||||
image = Image.new("RGB", rgba.size, "white")
|
||||
image.paste(rgba, mask=rgba.getchannel("A"))
|
||||
rgba.close()
|
||||
oriented.close()
|
||||
try:
|
||||
for name, output, format, options in (
|
||||
("thumbnail", fit(image, 256), "JPEG", {"quality": 85, "subsampling": 0}),
|
||||
(
|
||||
"crop",
|
||||
ImageOps.fit(image, (128, 128), method=Image.Resampling.LANCZOS),
|
||||
"JPEG",
|
||||
{"quality": 85, "subsampling": 0},
|
||||
),
|
||||
(
|
||||
"optimized",
|
||||
fit(image, 1280),
|
||||
"WEBP",
|
||||
{"quality": 82 if version == 1 else 76, "method": 4},
|
||||
),
|
||||
):
|
||||
with output, io.BytesIO() as buffer:
|
||||
output.save(buffer, format=format, exif=b"", **options)
|
||||
yield (
|
||||
name,
|
||||
buffer.getvalue(),
|
||||
output.size,
|
||||
"image/webp" if format == "WEBP" else "image/jpeg",
|
||||
)
|
||||
finally:
|
||||
image.close()
|
||||
Reference in New Issue
Block a user