A Python tool that uses Google's Gemini API to generate animated GIFs from text prompts
Project description
Gemini GIF Generator
A Python tool that uses Google's Gemini API to generate animated GIFs from text prompts.
๐ Inspired by @Miyamura80's gist
Features
- Generate animated GIFs using Google's Gemini 2.0 Flash model
- Customize animation subject, style, and frame rate
- Automatic retry logic to ensure multiple frames are generated
- Command-line interface with customizable parameters
- Support for storing API key in .env file for convenience
- Progress bars for better user experience
- Programmatic API for integration into other projects
Requirements
- Python 3.10+
- FFmpeg (system installation)
- Google Gemini API key
Installation
Using pip (Recommended)
# Install directly from PyPI
pip install gemini-gif
From Source
# Clone the repository
git clone https://github.com/yourusername/gemini-gif.git
cd gemini-gif
# Install in development mode
pip install -e .
Using Conda Environment
# Clone the repository
git clone https://github.com/yourusername/gemini-gif.git
cd gemini-gif
# Create and activate the conda environment from the environment.yml file
conda env create -f environment.yml
conda activate gemini-gif
# Install in development mode
pip install -e .
System Requirements
Make sure FFmpeg is installed on your system:
- macOS:
brew install ffmpeg - Ubuntu/Debian:
sudo apt-get install ffmpeg - Windows: Download from FFmpeg website or use Chocolatey:
choco install ffmpeg
API Key Setup
You can provide your Gemini API key in several ways:
Using a .env File (Recommended)
Create a file named .env in the project directory with the following content:
GEMINI_API_KEY=your_api_key_here
The script will automatically load the API key from this file.
Using Environment Variables
# Set your Gemini API key as an environment variable
export GEMINI_API_KEY="your_api_key_here"
Using Command-line Arguments
# Provide the API key directly as a command-line argument
gemini-gif --api-key "your_api_key_here" --subject "your subject"
Usage
Using the Command-line Interface
After installation, you can use the gemini-gif command:
# Generate a GIF with default settings (dancing cat in pixel art style)
gemini-gif
# Generate a GIF with custom subject and style
gemini-gif --subject "a dancing robot" --style "in a neon cyberpunk style"
Using the Shell Script
For convenience, you can use the provided shell script (if you installed from source):
# Generate a GIF with default settings
./generate_gif.sh
# Generate a GIF with custom subject and style
./generate_gif.sh --subject "a dancing robot" --style "in a neon cyberpunk style"
Command-line Options
gemini-gif --help
Available options:
--api-key: Google Gemini API key (can also be set via GEMINI_API_KEY environment variable)--subject: Subject of the animation (default: "A cute dancing cat")--style: Style of the animation (default: "in a 8-bit pixel art style")--template: Template for the prompt (default: "Create an animation by generating multiple frames, showing")--framerate: Frames per second for the output GIF (default: 2)--output: Output file path (default: animation_.gif)--max-retries: Maximum number of retries for generating frames (default: 3)--model: Gemini model to use (default: "models/gemini-2.0-flash-exp")--log-file: Path to the log file (default: gemini_gif_generator.log)--verbose: Enable verbose output
Examples
# Generate a blooming flower animation
gemini-gif --subject "a seed growing into a plant and then blooming into a flower" --style "in a watercolor style"
# Create a rocket launch animation with custom frame rate
gemini-gif --subject "a rocket launching into space" --style "in a retro sci-fi style" --framerate 3
# Save to a specific output file
gemini-gif --subject "a butterfly emerging from a cocoon" --output butterfly_animation.gif
Programmatic Usage
You can also use the package programmatically in your own Python code:
import os
import tempfile
from gemini_gif.core import config, generator, processor
# Set up logging and load environment variables
config.setup_logger()
config.load_env_variables()
# Get API key from environment
api_key = os.environ.get("GEMINI_API_KEY")
if not api_key:
raise ValueError("No API key found. Please set the GEMINI_API_KEY environment variable.")
# Initialize Gemini client
client = generator.initialize_client(api_key)
# Construct the prompt
subject = "A cute dancing cat"
style = "in a 8-bit pixel art style"
prompt = f"{config.DEFAULT_TEMPLATE} {subject} {style}"
print(f"Using prompt: {prompt}")
# Generate frames
response = generator.generate_frames(client, prompt)
# Create a temporary directory to store the frames
with tempfile.TemporaryDirectory() as temp_dir:
# Extract frames from the response
frame_paths, text_content = processor.extract_frames(response, temp_dir)
# Create the GIF
output_path = "my_animation.gif"
if frame_paths:
if processor.create_gif_from_frames(frame_paths, output_path):
print(f"GIF created successfully: {output_path}")
processor.open_gif(output_path)
See the examples/programmatic_usage.py file for a complete example.
Development
Project Structure
gemini-gif/
โโโ gemini_gif/ # Main package
โ โโโ __init__.py # Package initialization
โ โโโ cli.py # Command-line interface
โ โโโ core/ # Core functionality
โ โโโ __init__.py
โ โโโ config.py # Configuration handling
โ โโโ generator.py # Frame generation
โ โโโ main.py # Main process
โ โโโ processor.py # Frame processing and GIF creation
โโโ tests/ # Test directory
โ โโโ __init__.py
โ โโโ test_config.py # Tests for config module
โโโ examples/ # Example scripts
โ โโโ programmatic_usage.py # Example of programmatic usage
โโโ .env # Environment variables (not in git)
โโโ .gitignore # Git ignore file
โโโ LICENSE # MIT License
โโโ README.md # This file
โโโ environment.yml # Conda environment file
โโโ pyproject.toml # Python project configuration
โโโ setup.py # Package setup script
Running Tests
# Install test dependencies
pip install pytest
# Run tests
pytest
Troubleshooting
- If you encounter issues with the Gemini API, check your API key and ensure you have access to the Gemini 2.0 Flash model.
- If FFmpeg fails, ensure it's properly installed and accessible in your PATH.
- For any other issues, check the log file (
gemini_gif_generator.log) for detailed error messages.
License
This project is open source and available under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gemini_gif-0.1.0.tar.gz.
File metadata
- Download URL: gemini_gif-0.1.0.tar.gz
- Upload date:
- Size: 16.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c88c5ee6f741afd5328b745e2c75301c6f8f7340bf0a30d75e11b9a73e438e48
|
|
| MD5 |
964bf7e8e774aa30a0019fdde6d4c012
|
|
| BLAKE2b-256 |
76bcb32fea294507f87d433a4404365734e990341fcd4b6c6ed1c7988162df5f
|
File details
Details for the file gemini_gif-0.1.0-py3-none-any.whl.
File metadata
- Download URL: gemini_gif-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
89fb81e1b166a78e53f7fb8fb134293735d8cbe45c8bf124cda6bca290a2ad22
|
|
| MD5 |
ab033a09ee5efc926c6e77330756acdb
|
|
| BLAKE2b-256 |
cb05d0efac33541ecff1fac350e29a63ad328f301a49debd89ad54fbedd926d5
|