Spaces:
Sleeping
Sleeping
| from pathlib import Path | |
| import numpy as np | |
| from PIL import Image | |
| from skimage import exposure | |
| def normalize_image( | |
| src: Path, | |
| dst: Path, | |
| target_size=(512, 512), | |
| png_low: int = 16, | |
| png_high: int = 238, | |
| ) -> None: | |
| """Convert JPEG to grayscale, resize, and match PNG intensity distribution.""" | |
| img = Image.open(src).convert("L") | |
| img = img.resize(target_size, Image.Resampling.BICUBIC) | |
| arr = np.array(img, dtype=np.float32) | |
| arr = exposure.rescale_intensity( | |
| arr, in_range="image", out_range=(png_low, png_high) | |
| ) | |
| arr = np.clip(arr, png_low, png_high) | |
| arr = ((arr - png_low) / (png_high - png_low) * 255.0).astype(np.uint8) | |
| dst.parent.mkdir(parents=True, exist_ok=True) | |
| Image.fromarray(arr, mode="L").save(dst) | |
| if __name__ == "__main__": | |
| src = Path("/Users/fatemehtahavori/Downloads/atk1/K-1.jpg") | |
| dst = Path("normalized_outputs") / f"{src.stem}_normalized.png" | |
| normalize_image(src, dst) | |
| print(f"Saved normalized frame to {dst.resolve()}") | |