File size: 961 Bytes
76b4917
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

import os
import requests
from io import BytesIO
from PIL import Image
import numpy as np

def load_img(source, output_type="pil"):
    """

    Load an image from a local file path or a URL.



    Parameters:

    - source (str): A file path or a URL.

    - output_type (str): The output format: "pil" (PIL Image) or "numpy" (NumPy array).



    Returns:

    - PIL.Image.Image or numpy.ndarray depending on output_type.

    """

    # Determine if `source` is a local file path or a URL
    if os.path.exists(source):
        # Local file
        img = Image.open(source)
    else:
        # Assume source is a URL
        response = requests.get(source)
        response.raise_for_status()
        img = Image.open(BytesIO(response.content))

    if output_type == "pil":
        return img
    elif output_type == "numpy":
        return np.array(img)
    else:
        raise ValueError(f"Unknown output_type: {output_type}")