Add /api/comfyui/image endpoint for fetching generated images
Allows web clients to retrieve images by filename after generation. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,7 @@ import logging
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from fastapi import APIRouter, HTTPException, Query
|
from fastapi import APIRouter, HTTPException, Query
|
||||||
|
from fastapi.responses import Response
|
||||||
from pydantic import BaseModel as PydanticBaseModel
|
from pydantic import BaseModel as PydanticBaseModel
|
||||||
from pydantic import Field
|
from pydantic import Field
|
||||||
|
|
||||||
@@ -13,6 +14,7 @@ from tensors.comfyui import (
|
|||||||
clear_queue,
|
clear_queue,
|
||||||
generate_image,
|
generate_image,
|
||||||
get_history,
|
get_history,
|
||||||
|
get_image,
|
||||||
get_loaded_models,
|
get_loaded_models,
|
||||||
get_queue_status,
|
get_queue_status,
|
||||||
get_system_stats,
|
get_system_stats,
|
||||||
@@ -272,6 +274,30 @@ def comfyui_workflow(request: WorkflowRequest) -> dict[str, Any]:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/image/{filename}")
|
||||||
|
def comfyui_image(
|
||||||
|
filename: str,
|
||||||
|
subfolder: str = Query(default="", description="Subfolder within output directory"),
|
||||||
|
folder_type: str = Query(default="output", description="Folder type: output, input, temp"),
|
||||||
|
) -> Response:
|
||||||
|
"""Fetch a generated image from ComfyUI.
|
||||||
|
|
||||||
|
Use this to retrieve images by filename after generation.
|
||||||
|
"""
|
||||||
|
image_data = get_image(filename=filename, subfolder=subfolder, folder_type=folder_type)
|
||||||
|
if image_data is None:
|
||||||
|
raise HTTPException(status_code=404, detail="Image not found")
|
||||||
|
|
||||||
|
# Determine content type from filename
|
||||||
|
content_type = "image/png"
|
||||||
|
if filename.lower().endswith(".jpg") or filename.lower().endswith(".jpeg"):
|
||||||
|
content_type = "image/jpeg"
|
||||||
|
elif filename.lower().endswith(".webp"):
|
||||||
|
content_type = "image/webp"
|
||||||
|
|
||||||
|
return Response(content=image_data, media_type=content_type)
|
||||||
|
|
||||||
|
|
||||||
def create_comfyui_api_router() -> APIRouter:
|
def create_comfyui_api_router() -> APIRouter:
|
||||||
"""Return the ComfyUI API router."""
|
"""Return the ComfyUI API router."""
|
||||||
return router
|
return router
|
||||||
|
|||||||
Reference in New Issue
Block a user