MCPcopy
hub / github.com/ttengwang/Caption-Anything / load_image

Function load_image

caption_anything/utils/utils.py:16–40  ·  view source on GitHub ↗

Load image from path or PIL.Image or numpy.ndarray to required format.

(image: Union[np.ndarray, Image.Image, str], return_type='numpy')

Source from the content-addressed store, hash-verified

14
15
16def 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
43def image_resize(image: Image.Image, res=1024):

Callers 14

parse_dense_captionMethod · 0.90
parse_ocrMethod · 0.90
filter_captionMethod · 0.90
inference_boxMethod · 0.90
inference_segMethod · 0.90
inferenceMethod · 0.90
inferenceMethod · 0.90
inferenceMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected