Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gemini_gif-0.1.0.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gemini_gif-0.1.0-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

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

Hashes for gemini_gif-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c88c5ee6f741afd5328b745e2c75301c6f8f7340bf0a30d75e11b9a73e438e48
MD5 964bf7e8e774aa30a0019fdde6d4c012
BLAKE2b-256 76bcb32fea294507f87d433a4404365734e990341fcd4b6c6ed1c7988162df5f

See more details on using hashes here.

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

Hashes for gemini_gif-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 89fb81e1b166a78e53f7fb8fb134293735d8cbe45c8bf124cda6bca290a2ad22
MD5 ab033a09ee5efc926c6e77330756acdb
BLAKE2b-256 cb05d0efac33541ecff1fac350e29a63ad328f301a49debd89ad54fbedd926d5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page