c
import logging
import requests
from io import BytesIO
from PIL import Image
from fastapi import FastAPI, HTTPException
from fastapi.responses import RedirectResponse
from pydantic import BaseModel
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = FastAPI()
class ImageURL(BaseModel):
url: str
@app.get("/", include_in_schema=False)
def root():
return RedirectResponse(url="/docs")
@app.post("/analyze_image")
c
Created: | Last Updated:
Introduction to the ColorText API Template
The ColorText API template allows you to extract dominant colors from images and recommend the optimal text color for overlay. This can be particularly useful for ensuring text readability over images in various applications.
Getting Started
To begin using the ColorText API template, follow these steps:
- Click "Start with this Template": This will load the template into the Lazy Builder interface.
Testing the Template
- Press the Test Button: This will deploy the app and launch the Lazy CLI. The CLI will guide you through any required user input.
Using the API
Once the app is deployed, you can interact with the API. The FastAPI framework provides a convenient documentation interface.
- Access the API Documentation: After deployment, Lazy will provide a link to the FastAPI docs. You can use this link to explore the API endpoints and test them directly from the browser.
API Endpoint
Analyze Image
- Endpoint:
/analyze_image
- Method: POST
- Request Body: JSON object containing the image URL.
json
{
"url": "https://example.com/image.jpg"
}
- Response: JSON object containing the recommended text color.
json
{
"textColor": "black"
}
Sample Request
To analyze an image, you can use the following sample request:
bash
curl -X POST "http://<your-app-url>/analyze_image" -H "Content-Type: application/json" -d '{"url": "https://example.com/image.jpg"}'
Sample Response
The response will be a JSON object indicating the recommended text color:
json
{
"textColor": "black"
}
Integrating the App
If you need to integrate this API into another service, follow these steps:
- Get the Server Link: After pressing the Test button, Lazy will provide a dedicated server link for your deployed app.
- Use the API Endpoint: Use the provided server link to make requests to the
/analyze_image
endpoint as shown in the sample request.
By following these steps, you can easily deploy and integrate the ColorText API into your applications to ensure optimal text readability over images.