lab3_and_3grade

This commit is contained in:
caramelteawithmint
2026-11-13 18:01:54 +03:00
parent ec540a1a72
commit 09e26c3c5a
28 changed files with 1494 additions and 0 deletions
+24
View File
@@ -0,0 +1,24 @@
import io
from PIL import Image
def detect_image_type(data: bytes) -> str | None:
if data.startswith(b"\x89PNG\r\n\x1a\n"):
return "image/png"
if data.startswith(b"\xff\xd8\xff"):
return "image/jpeg"
return None
def check_image(data: bytes, allowed_types: list[str]) -> tuple[str, int, int]:
content_type = detect_image_type(data)
if not content_type or content_type not in allowed_types:
raise ValueError("Invalid or unsupported image type")
try:
with Image.open(io.BytesIO(data)) as img:
width, height = img.size
if width * height > 40_000_000:
raise ValueError("Too many pixels")
except Exception as e:
raise ValueError(f"Broken image: {e}")
return content_type, width, height