Simple and clean Python client for Google Cloud Vertex AI Imagen
Project description
🎨 Vertex AI Imagen Python Client
Simple and clean Python client for Google Cloud Vertex AI Imagen
Generate AI images with just a few lines of code!
🚀 Quick Start
Installation
pip install vertex-ai-imagen
Basic Usage
import asyncio
from vertex_ai_imagen import ImagenClient
async def main():
# Initialize client
client = ImagenClient(project_id="your-project-id")
client.setup_credentials("path/to/service-account-key.json")
# Generate image
image = await client.generate(
prompt="A beautiful sunset over the ocean",
aspect_ratio="16:9"
)
# Save image
image.save("sunset.png")
print(f"Image saved! Size: {image.size:,} bytes")
# Run
asyncio.run(main())
✨ Features
- 🚀 Simple API: Generate images with just a few lines
- 🎯 Type Safe: Full type hints support
- 🔒 Secure: Google Cloud service account authentication
- 📦 Clean Models: Intuitive data classes
- ⚡ Async Support: Built-in async/await patterns
- 🎛️ Full Control: Access to all Imagen parameters
📖 Advanced Usage
Multiple Images
# Generate multiple images
images = await client.generate(
prompt="A futuristic city with flying cars",
model="imagen-3.0-fast-generate-001",
aspect_ratio="16:9",
count=3,
negative_prompt="blurry, low quality",
seed=12345
)
# Save all images
for i, image in enumerate(images):
image.save(f"city_{i+1}.png")
Authentication Options
# Method 1: Direct key file
client.setup_credentials("path/to/key.json")
# Method 2: Environment variable (GOOGLE_APPLICATION_CREDENTIALS)
client.setup_credentials_from_env()
Supported Models
# List available models
models = client.list_models()
print(models)
# Output:
# ['imagegeneration@006', 'imagen-3.0-generate-001', 'imagen-3.0-fast-generate-001', ...]
Model | Speed | Quality | Use Case |
---|---|---|---|
imagen-3.0-fast-generate-001 |
⚡ Fast | 🟢 Good | Prototyping, batch generation |
imagegeneration@006 |
🟡 Medium | 🔵 Great | General purpose |
imagen-3.0-generate-002 |
🟡 Medium | 🟣 Best | High-quality work |
Aspect Ratios
1:1
- Square16:9
- Widescreen9:16
- Portrait (mobile)4:3
- Traditional landscape3:4
- Traditional portrait
🔧 Setup
Google Cloud Setup
-
Enable the API
gcloud services enable aiplatform.googleapis.com
-
Create Service Account
gcloud iam service-accounts create imagen-client \ --display-name="Imagen Client" gcloud projects add-iam-policy-binding PROJECT_ID \ --member="serviceAccount:imagen-client@PROJECT_ID.iam.gserviceaccount.com" \ --role="roles/aiplatform.user"
-
Create Service Account Key
gcloud iam service-accounts keys create key.json \ --iam-account=imagen-client@PROJECT_ID.iam.gserviceaccount.com
Environment Variables
export GOOGLE_CLOUD_PROJECT="your-project-id"
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/key.json"
📝 Examples
Check out the examples directory for more usage patterns:
- Basic Usage - Simple image generation
- Advanced Usage - Complete feature demo
Jupyter Notebook
# Display image directly in notebook
image = await client.generate("A cute cat")
image.show() # Shows image inline
Error Handling
from vertex_ai_imagen.exceptions import ImagenError, AuthenticationError
try:
image = await client.generate("A beautiful landscape")
image.save("landscape.png")
except AuthenticationError:
print("Please check your credentials")
except ImagenError as e:
print(f"Image generation failed: {e}")
📊 API Reference
ImagenClient
client = ImagenClient(
project_id="your-project-id",
location="us-central1" # optional
)
Generate Images
await client.generate(
prompt="Image description", # required
model="imagegeneration@006", # optional
aspect_ratio="1:1", # optional
count=1, # 1-4
negative_prompt="Things to exclude", # optional
seed=12345, # optional
safety_setting="block_medium_and_above", # optional
enhance_prompt=True # optional
)
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
📄 License
MIT License - see LICENSE file for details.
🔗 Links
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
Built Distribution
File details
Details for the file vertex_ai_imagen-1.1.4.tar.gz
.
File metadata
- Download URL: vertex_ai_imagen-1.1.4.tar.gz
- Upload date:
- Size: 13.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
5d3e9163db8680003bf5ab5623be2daaa21e01f5c76291f24795148e14d04c36
|
|
MD5 |
06adc927af01b02af23413c289f1229a
|
|
BLAKE2b-256 |
193ede87a9ecb325d1fbaa3bb2f869f5292fdb2789f3266d78b54e0881e04bea
|
Provenance
The following attestation bundles were made for vertex_ai_imagen-1.1.4.tar.gz
:
Publisher:
publish-pypi.yml
on realcoding2003/vertex-ai-imagen
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1
-
Predicate type:
https://docs.pypi.org/attestations/publish/v1
-
Subject name:
vertex_ai_imagen-1.1.4.tar.gz
-
Subject digest:
5d3e9163db8680003bf5ab5623be2daaa21e01f5c76291f24795148e14d04c36
- Sigstore transparency entry: 230540337
- Sigstore integration time:
-
Permalink:
realcoding2003/vertex-ai-imagen@8b59a01c57f28650e616408eec2deab9d56f0b69
-
Branch / Tag:
refs/tags/v1.1.4
- Owner: https://github.com/realcoding2003
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com
-
Runner Environment:
github-hosted
-
Publication workflow:
publish-pypi.yml@8b59a01c57f28650e616408eec2deab9d56f0b69
-
Trigger Event:
release
-
Statement type:
File details
Details for the file vertex_ai_imagen-1.1.4-py3-none-any.whl
.
File metadata
- Download URL: vertex_ai_imagen-1.1.4-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
5dd5356320f542785c2a5ec120a9402a5536cb995effb212bf6d00c8ed62750e
|
|
MD5 |
4ab3cc1a6eee290065ff446550ba968e
|
|
BLAKE2b-256 |
fcf1b15f447b545ecf95e9b40305f2ab2b63ca2f66a7feb921978666d73e6d2d
|
Provenance
The following attestation bundles were made for vertex_ai_imagen-1.1.4-py3-none-any.whl
:
Publisher:
publish-pypi.yml
on realcoding2003/vertex-ai-imagen
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1
-
Predicate type:
https://docs.pypi.org/attestations/publish/v1
-
Subject name:
vertex_ai_imagen-1.1.4-py3-none-any.whl
-
Subject digest:
5dd5356320f542785c2a5ec120a9402a5536cb995effb212bf6d00c8ed62750e
- Sigstore transparency entry: 230540338
- Sigstore integration time:
-
Permalink:
realcoding2003/vertex-ai-imagen@8b59a01c57f28650e616408eec2deab9d56f0b69
-
Branch / Tag:
refs/tags/v1.1.4
- Owner: https://github.com/realcoding2003
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com
-
Runner Environment:
github-hosted
-
Publication workflow:
publish-pypi.yml@8b59a01c57f28650e616408eec2deab9d56f0b69
-
Trigger Event:
release
-
Statement type: