Parse input to determine if it's an image file. Args: s: Input to parse (string path, PIL Image, list, or other). image_name: Name to use for image type identification. Returns: Tuple of (mime_type, value) where mime_type is None for non-images.
(s: Any, image_name: str = "image")
| 102 | |
| 103 | |
| 104 | def parse_file(s: Any, image_name: str = "image") -> Tuple[Optional[str], Any]: |
| 105 | """Parse input to determine if it's an image file. |
| 106 | |
| 107 | Args: |
| 108 | s: Input to parse (string path, PIL Image, list, or other). |
| 109 | image_name: Name to use for image type identification. |
| 110 | |
| 111 | Returns: |
| 112 | Tuple of (mime_type, value) where mime_type is None for non-images. |
| 113 | """ |
| 114 | if isinstance(s, list): |
| 115 | return (image_name, s) |
| 116 | elif isinstance(s, Image.Image): |
| 117 | return (image_name, s) |
| 118 | elif isinstance(s, str): |
| 119 | # Check base64 data URL before osp.exists() to avoid stat() |
| 120 | # syscall on multi-MB strings. |
| 121 | if s.startswith("data:image"): |
| 122 | return (image_name, s) |
| 123 | elif osp.exists(s) and s != "." and osp.isfile(s): |
| 124 | return (image_name, s) |
| 125 | return (None, s) |
| 126 | |
| 127 | |
| 128 | def load_and_resize_image(img_path: str, resize_size: int): |