Spaces:
Running on Zero
Running on Zero
Download source/preprocessing.py from Manish18/ai-image-detector: direct link, hf CLI and curl.
- Browser
- Download file 739 Bytes
-
https://huggingface.co/spaces/Manish18/ai-image-detector/resolve/main/source/preprocessing.py
- Command line
-
hf download hf://spaces/Manish18/ai-image-detector/source/preprocessing.py
-
curl -L -o preprocessing.py https://huggingface.co/spaces/Manish18/ai-image-detector/resolve/main/source/preprocessing.py
739 Bytes
| 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 |