Download utils.py from MeesamBukhari/Satellite-Name-Generator: direct link, hf CLI and curl.
- Browser
- Download file 2.61 kB
-
https://huggingface.co/spaces/MeesamBukhari/Satellite-Name-Generator/resolve/main/utils.py
- Command line
-
hf download hf://spaces/MeesamBukhari/Satellite-Name-Generator/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/MeesamBukhari/Satellite-Name-Generator/resolve/main/utils.py
2.61 kB
| import os | |
| import random | |
| import tempfile | |
| from PIL import Image, ImageEnhance, ImageDraw | |
| def enhance_image(img): | |
| """Applies slight brightness and contrast enhancements for visual pop.""" | |
| # Enhance Contrast | |
| contrast = ImageEnhance.Contrast(img) | |
| img = contrast.enhance(1.15) | |
| # Enhance Color | |
| color = ImageEnhance.Color(img) | |
| img = color.enhance(1.2) | |
| return img | |
| def stitch_images(images, size=256): | |
| """Combines a list of PIL Images horizontally.""" | |
| if not images: | |
| return None | |
| images = [img.resize((size, size), Image.Resampling.LANCZOS) for img in images] | |
| total_width = size * len(images) | |
| max_height = size | |
| stitched_img = Image.new('RGB', (total_width, max_height)) | |
| x_offset = 0 | |
| for im in images: | |
| stitched_img.paste(im, (x_offset, 0)) | |
| x_offset += size | |
| return stitched_img | |
| def save_temp_image(img): | |
| """Saves PIL Image to a temporary file and returns path for Gradio DownloadButton.""" | |
| if img is None: | |
| return None | |
| temp_dir = tempfile.mkdtemp() | |
| path = os.path.join(temp_dir, "nasa_satellite_name.png") | |
| img.save(path) | |
| return path | |
| def setup_dummy_dataset(data_dir="data"): | |
| """ | |
| Creates a dummy dataset if missing. Ensures the Gradio app runs immediately | |
| even before the user provides real satellite imagery. | |
| """ | |
| if os.path.exists(data_dir) and len(os.listdir(data_dir)) > 0: | |
| return # Dataset exists | |
| print(f"'{data_dir}' folder not found or empty. Generating dummy satellite dataset...") | |
| import string | |
| os.makedirs(data_dir, exist_ok=True) | |
| for char in string.ascii_uppercase: | |
| char_dir = os.path.join(data_dir, char) | |
| os.makedirs(char_dir, exist_ok=True) | |
| # Create 10 mock images per letter | |
| for i in range(10): | |
| # Mock satellite terrain colors | |
| bg_color = (random.randint(40, 80), random.randint(80, 120), random.randint(40, 80)) | |
| img = Image.new('RGB', (256, 256), color=bg_color) | |
| draw = ImageDraw.Draw(img) | |
| # Draw a crude representation of the letter so CLIP has *something* to recognize | |
| draw.text((80, 50), char, fill=(150, 200, 150), font_size=120) | |
| # Add noise to mimic satellite | |
| for _ in range(500): | |
| x, y = random.randint(0, 255), random.randint(0, 255) | |
| draw.point((x, y), fill=(200, 200, 200)) | |
| img.save(os.path.join(char_dir, f"{char}_{i}.jpg")) | |
| print("Dummy dataset created.") |