Skip to main content

TextFromImage

Python Version PyPI Version License Downloads

A powerful Python library for obtaining detailed descriptions of images using various AI models including OpenAI's GPT models, Azure OpenAI, and Anthropic Claude. Perfect for applications requiring image understanding, accessibility features, and content analysis. Supports both local files and URLs, with batch processing capabilities.

🌟 Key Features

  • 🤖 Multiple AI Providers: Support for OpenAI, Azure OpenAI, and Anthropic Claude
  • 🌐 Flexible Input: Support for both URLs and local file paths
  • 📦 Batch Processing: Process multiple images (up to 20) concurrently
  • 🔄 Flexible Integration: Easy-to-use API with multiple initialization options
  • 🎯 Custom Prompting: Configurable prompts for targeted descriptions
  • 🔑 Secure Authentication: Multiple authentication methods including environment variables
  • 🛠️ Model Selection: Support for different model versions and configurations
  • 📝 Type Hints: Full typing support for better development experience

📦 Installation

pip install textfromimage

# With Azure support
pip install textfromimage[azure]

# With all optional dependencies
pip install textfromimage[all]

🚀 Quick Start

import textfromimage

# Initialize with API key
textfromimage.openai.init(api_key="your-openai-api-key")

# Process single image (URL or local file)
image_url = 'https://example.com/image.jpg'
local_image = '/path/to/local/image.jpg'

# Get description from URL
url_description = textfromimage.openai.get_description(image_path=image_url)

# Get description from local file
local_description = textfromimage.openai.get_description(image_path=local_image)

# Batch processing
image_paths = [
    'https://example.com/image1.jpg',
    '/path/to/local/image2.jpg',
    'https://example.com/image3.jpg'
]

batch_results = textfromimage.openai.get_description_batch(
    image_paths=image_paths,
    concurrent_limit=3  # Process 3 images at a time
)

# Process results
for result in batch_results:
    if result.success:
        print(f"Success for {result.image_path}: {result.description}")
    else:
        print(f"Failed for {result.image_path}: {result.error}")

💡 Advanced Usage

🤖 Multiple Provider Support

# Anthropic Claude Integration
textfromimage.claude.init(api_key="your-anthropic-api-key")

# Single image
claude_description = textfromimage.claude.get_description(
    image_path=image_path,
    model="claude-3-sonnet-20240229"
)

# Batch processing
claude_results = textfromimage.claude.get_description_batch(
    image_paths=image_paths,
    model="claude-3-sonnet-20240229",
    concurrent_limit=3
)

# Azure OpenAI Integration
textfromimage.azure_openai.init(
    api_key="your-azure-openai-api-key",
    api_base="https://your-azure-endpoint.openai.azure.com/",
    deployment_name="your-deployment-name"
)

# Single image with system prompt
azure_description = textfromimage.azure_openai.get_description(
    image_path=image_path,
    system_prompt="Analyze this image in detail"
)

# Batch processing
azure_results = textfromimage.azure_openai.get_description_batch(
    image_paths=image_paths,
    system_prompt="Analyze each image in detail",
    concurrent_limit=3
)

🔧 Configuration Options

# Environment Variable Configuration
import os
os.environ['OPENAI_API_KEY'] = 'your-openai-api-key'
os.environ['ANTHROPIC_API_KEY'] = 'your-anthropic-api-key'
os.environ['AZURE_OPENAI_API_KEY'] = 'your-azure-openai-api-key'
os.environ['AZURE_OPENAI_ENDPOINT'] = 'your-azure-endpoint'
os.environ['AZURE_OPENAI_DEPLOYMENT'] = 'your-deployment-name'

# Custom options for batch processing
batch_results = textfromimage.openai.get_description_batch(
    image_paths=image_paths,
    model='gpt-4-vision-preview',
    prompt="Describe the main elements of each image",
    max_tokens=300,
    concurrent_limit=5
)

📋 Parameters and Types

# Single image processing parameters
def get_description(
    image_path: str,
    prompt: str = "What's in this image?",
    max_tokens: int = 300,
    model: str = "gpt-4-vision-preview"
) -> str: ...

# Batch processing result type
@dataclass
class BatchResult:
    success: bool
    description: Optional[str]
    error: Optional[str]
    image_path: str

# Batch processing parameters
def get_description_batch(
    image_paths: List[str],
    prompt: str = "What's in this image?",
    max_tokens: int = 300,
    model: str = "gpt-4-vision-preview",
    concurrent_limit: int = 3
) -> List[BatchResult]: ...

🔍 Error Handling

from textfromimage.utils import BatchResult

# Single image processing
try:
    description = textfromimage.openai.get_description(image_path=image_path)
except ValueError as e:
    print(f"Image processing error: {e}")
except RuntimeError as e:
    print(f"API error: {e}")

# Batch processing error handling
results = textfromimage.openai.get_description_batch(image_paths)
successful = [r for r in results if r.success]
failed = [r for r in results if not r.success]

for result in failed:
    print(f"Failed to process {result.image_path}: {result.error}")

🤝 Contributing

We welcome contributions! Here's how you can help:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

📝 License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for textfromimage 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for textfromimage 1.1.1
File Size Uploaded Provenance Actions
textfromimage-1.1.1.tar.gz 10.7 kB Without provenance Details for textfromimage-1.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for textfromimage 1.1.1
File Interpreter ABI Platform Actions
textfromimage-1.1.1-py3-none-any.whl Python 3 none any Details for textfromimage-1.1.1-py3-none-any.whl

Total release size: 20.2 kB

Release files / textfromimage-1.1.1.tar.gz

Download URL textfromimage-1.1.1.tar.gz
Size 10.7 kB
Tags Source
SHA-256 checksum
How to use checksums
2bf355c52a768d3ee6c52bb05cda9175197d6d008daeb2c05d81f4c95fc3f257
BLAKE2b-256 checksum
How to use checksums
568570ce17d1f7b60a82ab2625af9ba2878027bddec835bb2ebf8791482c8a17
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release files / textfromimage-1.1.1-py3-none-any.whl

Download URL textfromimage-1.1.1-py3-none-any.whl
Size 9.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ecf3a7fa4cefe2b51336fa126f9af6ff80cc7a2c4812036617318e1ce1335770
BLAKE2b-256 checksum
How to use checksums
e345e77b2804d12c367087fd50c9509d25a41cf27192f65485a93701eaf4f786
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Security for textfromimage 1.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page