MCPcopy Index your code
hub / github.com/walter201230/Python / detect_image_kind

Function detect_image_kind

tools/download_legacy_images.py:28–44  ·  view source on GitHub ↗

返回 'png'/'jpeg'/'gif'/'webp' 或 None(不是合法图片)。

(path: Path)

Source from the content-addressed store, hash-verified

26
27
28def detect_image_kind(path: Path) -> str | None:
29 """返回 'png'/'jpeg'/'gif'/'webp' 或 None(不是合法图片)。"""
30 try:
31 head = path.read_bytes()[:16]
32 except Exception:
33 return None
34 if len(head) < 12:
35 return None
36 if head.startswith(b"\x89PNG\r\n\x1a\n"):
37 return "png"
38 if head.startswith(b"\xff\xd8\xff"):
39 return "jpeg"
40 if head[:4] == b"GIF8":
41 return "gif"
42 if head[:4] == b"RIFF" and head[8:12] == b"WEBP":
43 return "webp"
44 return None
45
46
47def download_one(url: str, local: Path, timeout: int = 15) -> tuple[bool, str]:

Callers 3

download_oneFunction · 0.85
cmd_downloadFunction · 0.85
cmd_validateFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected