Show HN: Auto-optimize and upload images with AI alt text
2 days ago
2
Created by Cagri Sarigoz | Open Source | MIT License
A comprehensive tool for downloading, optimizing, and uploading images to AWS S3 with CloudFront distribution. Features automatic image optimization, format conversion, unique URL generation, and AI-powered alt text generation for accessibility and SEO.
Batch Image Processing: Download images from URLs and upload to S3/CloudFront
Image Optimization: Automatic resizing, quality adjustment, and format conversion
Smart Format Selection: Automatically chooses the best format (JPEG, PNG, WebP) for optimal file size
Unique URLs: Adds timestamps to prevent filename conflicts and ensure cache busting
AI Alt Text Generation: Generate descriptive alt text using AltText.ai API
Windows:
Download from python.org or use Windows Store.
Verify installation:
python3.13 --version
# Should output: Python 3.13.x
2. Create and Activate Virtual Environment
Create virtual environment:
# Clone the repository first
git clone https://github.com/cagrisarigoz/image-optimization.git
cd image-optimization
# Create virtual environment with Python 3.13
python3.13 -m venv venv
# Alternative if python3.13 is your default python
python -m venv venv
Activate virtual environment:
Linux/macOS:
Windows (Command Prompt):
Windows (PowerShell):
venv\Scripts\Activate.ps1
Verify activation:
which python # Should point to venv/bin/python
python --version # Should show Python 3.13.x
# Upgrade pip to latest version
python -m pip install --upgrade pip
# Install project dependencies
pip install -r requirements.txt
# Copy environment template
cp env.example .env
# Edit .env with your credentials (use your preferred editor)
nano .env # or vim .env, code .env, etc.
Quick Start (Alternative Method)
If you prefer a simpler setup or already have Python 3.9+ installed:
Clone the repository:
git clone https://github.com/cagrisarigoz/image-optimization.git
cd image-optimization
Install dependencies:
pip install -r requirements.txt
Run setup:
Configure environment:
cp env.example .env
# Edit .env with your credentials
Virtual Environment Benefits
Using a virtual environment provides several advantages:
Isolation: Dependencies don't conflict with system packages
Reproducibility: Consistent environment across different machines
Version Control: Lock specific package versions
Clean Uninstall: Easy to remove by deleting the venv folder
Multiple Projects: Different Python versions for different projects
Deactivating Virtual Environment
When you're done working on the project:
Python 3.9+ (Tested on 3.9, 3.10, 3.11, 3.12, 3.13)
AWS account with S3 and CloudFront access
AltText.ai API key (optional, for alt text generation)
Interactive CSV Processing
fromupload_filesimportdownload_and_upload_from_csv# Process images with alt text generationdownload_and_upload_from_csv(
generate_alt_text=True,
alt_text_keywords="product, ecommerce, lifestyle"
)
# Start the API serverexport FLASK_RUN=1
python upload_files.py
# Upload a file
curl -X POST -F "[email protected]" http://localhost:5000/upload
# Interactive mode with prompts
./process_csv.sh
# Follow the prompts to configure:# - Alt text generation (Y/N)# - Keywords for SEO# - Image optimization settings# - Quality and format options
# Set environment variables for automationexport PROCESS_CSV=1
export GENERATE_ALT_TEXT=true
export ALT_TEXT_KEYWORDS="product, lifestyle, modern"export MAX_WIDTH=1200
export QUALITY=85
export SMART_FORMAT=true
python upload_files.py
fromupload_filesimportupload_files# Upload local files with optimizationupload_files(
max_width=800,
quality=82,
smart_format=True,
generate_alt_text=True
)
# Start the serverexport FLASK_RUN=1
python upload_files.py &# Upload single file
curl -X POST -F "[email protected]" http://localhost:5000/upload
# List uploaded files
curl http://localhost:5000/files
# Process CSV via API
curl http://localhost:5000/process-csv
Automatic descriptions: Generate descriptive alt text for accessibility
SEO optimization: Include custom keywords for better search rankings
Batch processing: Generate alt text for multiple images efficiently
Fallback handling: Graceful degradation when API is unavailable
# Enable alt text generation
ALTTEXT_AI_API_KEY=your_api_key
ALTTEXT_AI_KEYWORDS=product,ecommerce,lifestyle,modern
# Optional webhook for async processing
ALTTEXT_AI_WEBHOOK_URL=https://your-domain.com/webhook
fromalttext_aiimportgenerate_alt_text# Generate alt text for an imagealt_text=generate_alt_text(
image_url="https://example.com/image.jpg",
keywords="product, modern, lifestyle"
)
print(f"Generated alt text: {alt_text}")
The tool generates several output files in the data/output/ directory:
data/output/images_mapping.csv
Maps original URLs to optimized CloudFront URLs with alt text:
filename,alt_text
photo1.jpg,"Beautiful sunset over mountains"
product2.png,"Modern smartphone with sleek design"
Image Optimization Settings
# Customize optimization parametersoptimize_image(
image_path="photo.jpg",
max_width=1200, # Maximum width in pixelsquality=85, # JPEG/WebP quality (1-100)smart_format=True# Auto-select best format
)
# Test core functionality
python upload_files.py
# Test AltText.ai integration
python alttext_ai.py
# Test CSV processing
./process_csv.sh
# Test API endpointsexport FLASK_RUN=1
python upload_files.py &
curl -X POST -F "[email protected]" http://localhost:5000/upload
# Error: Python version not supported# Solution: Upgrade to Python 3.9+
python --version # Check current version# Install Python 3.13 (recommended)# See installation instructions above
Virtual Environment Issues
# Error: Command not found after activation# Solution: Ensure virtual environment is activatedsource venv/bin/activate # Linux/macOS# or
venv\Scripts\activate # Windows# Verify activation
which python # Should point to venv directory
# Error: AWS credentials not found# Solution: Check your .env file
AWS_ACCESS_KEY=your_key_here
AWS_SECRET_KEY=your_secret_here
# Error: AltText.ai connection failed# Solution: Verify your API key
python -c "from alttext_ai import test_alttext_ai_connection; test_alttext_ai_connection()"