import requests from PIL import Image REQUESTS_HEADERS = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' } def download_image_as_pil(url: str, timeout: int = 10) -> Image.Image: try: response = requests.get(url, stream=True, headers=REQUESTS_HEADERS, timeout=timeout) if response.status_code == 200: return Image.open(response.raw).convert("RGB") # Ensure consistent format return None except Exception as e: print(f"Error downloading image: {e}") return None