返回 'png'/'jpeg'/'gif'/'webp' 或 None(不是合法图片)。
(path: Path)
| 26 | |
| 27 | |
| 28 | def 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 | |
| 47 | def download_one(url: str, local: Path, timeout: int = 15) -> tuple[bool, str]: |
no outgoing calls
no test coverage detected