Load image from path or PIL.Image or numpy.ndarray to required format.
(image: Union[np.ndarray, Image.Image, str], return_type='numpy')
| 14 | |
| 15 | |
| 16 | def load_image(image: Union[np.ndarray, Image.Image, str], return_type='numpy'): |
| 17 | """ |
| 18 | Load image from path or PIL.Image or numpy.ndarray to required format. |
| 19 | """ |
| 20 | |
| 21 | # Check if image is already in return_type |
| 22 | if isinstance(image, Image.Image) and return_type == 'pil' or \ |
| 23 | isinstance(image, np.ndarray) and return_type == 'numpy': |
| 24 | return image |
| 25 | |
| 26 | # PIL.Image as intermediate format |
| 27 | if isinstance(image, str): |
| 28 | image = Image.open(image) |
| 29 | elif isinstance(image, np.ndarray): |
| 30 | image = Image.fromarray(image) |
| 31 | |
| 32 | if image.mode == "RGBA": |
| 33 | image = image.convert("RGB") |
| 34 | |
| 35 | if return_type == 'pil': |
| 36 | return image |
| 37 | elif return_type == 'numpy': |
| 38 | return np.asarray(image) |
| 39 | else: |
| 40 | raise NotImplementedError() |
| 41 | |
| 42 | |
| 43 | def image_resize(image: Image.Image, res=1024): |
no outgoing calls
no test coverage detected