forked from mtusi2026/mtusi2026lab5
all for 4
This commit is contained in:
@@ -0,0 +1,184 @@
|
||||
from pathlib import Path
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import zlib
|
||||
|
||||
from PIL import Image
|
||||
|
||||
|
||||
SCRIPT = Path(__file__).with_name("process_image.py")
|
||||
|
||||
|
||||
def execute(folder, expected):
|
||||
result = subprocess.run(
|
||||
[
|
||||
sys.executable,
|
||||
str(SCRIPT),
|
||||
str(folder / "input.bin"),
|
||||
str(folder),
|
||||
],
|
||||
capture_output=True,
|
||||
timeout=30,
|
||||
)
|
||||
|
||||
assert result.returncode == expected, (
|
||||
folder.name,
|
||||
result.returncode,
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
|
||||
cases = [
|
||||
(
|
||||
"alpha",
|
||||
(320, 200),
|
||||
"RGBA",
|
||||
(255, 0, 0, 0),
|
||||
None,
|
||||
[(256, 160), (128, 128), (320, 200)],
|
||||
),
|
||||
(
|
||||
"small",
|
||||
(20, 10),
|
||||
"RGB",
|
||||
"red",
|
||||
None,
|
||||
[(20, 10), (128, 128), (20, 10)],
|
||||
),
|
||||
(
|
||||
"orientation",
|
||||
(320, 200),
|
||||
"RGB",
|
||||
"blue",
|
||||
6,
|
||||
[(160, 256), (128, 128), (200, 320)],
|
||||
),
|
||||
]
|
||||
|
||||
for label, size, mode, color, orientation, expected in cases:
|
||||
folder = root / label
|
||||
folder.mkdir()
|
||||
|
||||
with Image.new(mode, size, color) as image:
|
||||
if orientation:
|
||||
exif = Image.Exif()
|
||||
exif[274] = orientation
|
||||
|
||||
image.save(
|
||||
folder / "input.bin",
|
||||
"JPEG",
|
||||
exif=exif,
|
||||
)
|
||||
else:
|
||||
image.save(folder / "input.bin", "PNG")
|
||||
|
||||
execute(folder, 0)
|
||||
|
||||
names = [
|
||||
"thumbnail.jpg",
|
||||
"crop.jpg",
|
||||
"optimized.webp",
|
||||
]
|
||||
|
||||
for name, dimensions in zip(names, expected):
|
||||
with Image.open(folder / name) as result:
|
||||
assert result.size == dimensions
|
||||
assert not result.getexif()
|
||||
|
||||
assert not any(
|
||||
result.info.get(key)
|
||||
for key in ["icc_profile", "xmp", "exif"]
|
||||
)
|
||||
|
||||
if label == "alpha" and name.endswith(".jpg"):
|
||||
assert result.getpixel((0, 0)) == (
|
||||
255,
|
||||
255,
|
||||
255,
|
||||
)
|
||||
|
||||
print("PASS:", label)
|
||||
|
||||
folder = root / "center_crop"
|
||||
folder.mkdir()
|
||||
|
||||
with Image.new("RGB", (400, 200), "blue") as image:
|
||||
image.paste("red", (0, 0, 100, 200))
|
||||
image.paste("green", (300, 0, 400, 200))
|
||||
image.save(folder / "input.bin", "PNG")
|
||||
|
||||
execute(folder, 0)
|
||||
|
||||
with Image.open(folder / "crop.jpg") as crop:
|
||||
for x in [32, 96]:
|
||||
red, green, blue = crop.getpixel((x, 64))
|
||||
|
||||
assert blue > 240
|
||||
assert red < 15
|
||||
assert green < 15
|
||||
|
||||
print("PASS: centered crop")
|
||||
|
||||
for label, data in [
|
||||
("broken", b"not an image"),
|
||||
("bytes_limit", b"x" * (10485760 + 1)),
|
||||
]:
|
||||
folder = root / label
|
||||
folder.mkdir()
|
||||
|
||||
(folder / "input.bin").write_bytes(data)
|
||||
|
||||
execute(folder, 10)
|
||||
|
||||
print("PASS rejected:", label)
|
||||
|
||||
folder = root / "pixels"
|
||||
folder.mkdir()
|
||||
|
||||
def chunk(kind, data):
|
||||
return (
|
||||
struct.pack(">I", len(data))
|
||||
+ kind
|
||||
+ data
|
||||
+ struct.pack(
|
||||
">I",
|
||||
zlib.crc32(kind + data) & 0xffffffff,
|
||||
)
|
||||
)
|
||||
|
||||
png = (
|
||||
b"\x89PNG\r\n\x1a\n"
|
||||
+ chunk(
|
||||
b"IHDR",
|
||||
struct.pack(
|
||||
">IIBBBBB",
|
||||
10000,
|
||||
10000,
|
||||
8,
|
||||
2,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
),
|
||||
)
|
||||
+ chunk(
|
||||
b"IDAT",
|
||||
zlib.compress(b"\0\0\0\0"),
|
||||
)
|
||||
+ chunk(b"IEND", b"")
|
||||
)
|
||||
|
||||
(folder / "input.bin").write_bytes(png)
|
||||
|
||||
execute(folder, 10)
|
||||
|
||||
print("PASS rejected: pixels before decode")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user