File size: 739 Bytes
6bec40d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import numpy as np
from PIL import Image

def load_and_preprocess_image(image_path, target_size=(224, 224)):
    """
    Loads an image from disk, converts to RGB, resizes, and normalizes it.
    
    Returns:
        img (PIL.Image): Original unresized PIL image.
        img_resized (PIL.Image): Resized PIL image (224x224).
        img_normalized (np.ndarray): Normalized image array scaled to float range [0.0, 1.0].
    """
    # 1. Format Conversion & Load
    img = Image.open(image_path).convert("RGB")
    
    # 2. Resize
    img_resized = img.resize(target_size)
    
    # 3. Normalization (Scaling to [0, 1])
    img_normalized = np.array(img_resized, dtype=np.float32) / 255.0
    
    return img, img_resized, img_normalized