Skip to content

Commit c328deb

Browse files
Merge pull request #3934 from bamarillo/api-add-png-info-endpoint
[API][Feature] Add png info endpoint
2 parents 9bb6b65 + 83a1f44 commit c328deb

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

modules/api/api.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from modules.api.models import *
66
from modules.processing import StableDiffusionProcessingTxt2Img, StableDiffusionProcessingImg2Img, process_images
77
from modules.sd_samplers import all_samplers
8-
from modules.extras import run_extras
8+
from modules.extras import run_extras, run_pnginfo
99

1010
def upscaler_to_index(name: str):
1111
try:
@@ -32,6 +32,7 @@ def __init__(self, app, queue_lock):
3232
self.app.add_api_route("/sdapi/v1/img2img", self.img2imgapi, methods=["POST"], response_model=ImageToImageResponse)
3333
self.app.add_api_route("/sdapi/v1/extra-single-image", self.extras_single_image_api, methods=["POST"], response_model=ExtrasSingleImageResponse)
3434
self.app.add_api_route("/sdapi/v1/extra-batch-images", self.extras_batch_images_api, methods=["POST"], response_model=ExtrasBatchImagesResponse)
35+
self.app.add_api_route("/sdapi/v1/png-info", self.pnginfoapi, methods=["POST"], response_model=PNGInfoResponse)
3536

3637
def text2imgapi(self, txt2imgreq: StableDiffusionTxt2ImgProcessingAPI):
3738
sampler_index = sampler_to_index(txt2imgreq.sampler_index)
@@ -125,8 +126,13 @@ def prepareFiles(file):
125126

126127
return ExtrasBatchImagesResponse(images=list(map(encode_pil_to_base64, result[0])), html_info=result[1])
127128

128-
def pnginfoapi(self):
129-
raise NotImplementedError
129+
def pnginfoapi(self, req: PNGInfoRequest):
130+
if(not req.image.strip()):
131+
return PNGInfoResponse(info="")
132+
133+
result = run_pnginfo(decode_base64_to_image(req.image.strip()))
134+
135+
return PNGInfoResponse(info=result[1])
130136

131137
def launch(self, server_name, port):
132138
self.app.include_router(self.router)

modules/api/models.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import inspect
2+
from click import prompt
23
from pydantic import BaseModel, Field, create_model
34
from typing import Any, Optional
45
from typing_extensions import Literal
@@ -148,4 +149,10 @@ class ExtrasBatchImagesRequest(ExtrasBaseRequest):
148149
imageList: list[FileData] = Field(title="Images", description="List of images to work on. Must be Base64 strings")
149150

150151
class ExtrasBatchImagesResponse(ExtraBaseResponse):
151-
images: list[str] = Field(title="Images", description="The generated images in base64 format.")
152+
images: list[str] = Field(title="Images", description="The generated images in base64 format.")
153+
154+
class PNGInfoRequest(BaseModel):
155+
image: str = Field(title="Image", description="The base64 encoded PNG image")
156+
157+
class PNGInfoResponse(BaseModel):
158+
info: str = Field(title="Image info", description="A string with all the info the image had")

0 commit comments

Comments
 (0)