Loads `image` to a PIL Image. Args: image (`str` or `PIL.Image.Image`): The image to convert to the PIL Image format. Returns: `PIL.Image.Image`: A PIL Image.
(image: Union[str, PIL.Image.Image])
| 367 | |
| 368 | |
| 369 | def load_image(image: Union[str, PIL.Image.Image]) -> PIL.Image.Image: |
| 370 | """ |
| 371 | Loads `image` to a PIL Image. |
| 372 | |
| 373 | Args: |
| 374 | image (`str` or `PIL.Image.Image`): |
| 375 | The image to convert to the PIL Image format. |
| 376 | Returns: |
| 377 | `PIL.Image.Image`: |
| 378 | A PIL Image. |
| 379 | """ |
| 380 | if isinstance(image, str): |
| 381 | if image.startswith("http://") or image.startswith("https://"): |
| 382 | image = PIL.Image.open(requests.get(image, stream=True).raw) |
| 383 | elif os.path.isfile(image): |
| 384 | image = PIL.Image.open(image) |
| 385 | else: |
| 386 | raise ValueError( |
| 387 | f"Incorrect path or url, URLs must start with `http://` or `https://`, and {image} is not a valid path" |
| 388 | ) |
| 389 | elif isinstance(image, PIL.Image.Image): |
| 390 | image = image |
| 391 | else: |
| 392 | raise ValueError( |
| 393 | "Incorrect format used for image. Should be an url linking to an image, a local path, or a PIL image." |
| 394 | ) |
| 395 | image = PIL.ImageOps.exif_transpose(image) |
| 396 | image = image.convert("RGB") |
| 397 | return image |
| 398 | |
| 399 | |
| 400 | def preprocess_image(image: PIL.Image, batch_size: int): |
no outgoing calls
no test coverage detected