fix issues with finding the path to the image file, no matter how the application was started

This commit is contained in:
Benjamin Berman 2023-08-11 15:26:13 -07:00
parent 040cc573d0
commit 95b630224d
3 changed files with 37 additions and 33 deletions

View File

@ -328,12 +328,12 @@ paths:
schema: schema:
type: string type: string
description: | description: |
The binary content of the last SaveImage node. The content of the last SaveImage node.
content: content:
text/uri-list: application/json:
schema: schema:
description: | description: |
The URI to retrieve the binary content of the image. A list of URLs to retrieve the binary content of the image.
This will return two URLs. The first is the ordinary ComfyUI view image URL that exactly corresponds This will return two URLs. The first is the ordinary ComfyUI view image URL that exactly corresponds
to the UI call. The second is the URL that corresponds to sha256 hash of the request body. to the UI call. The second is the URL that corresponds to sha256 hash of the request body.
@ -387,15 +387,14 @@ paths:
return hash_object.hexdigest() return hash_object.hexdigest()
``` ```
type: string type: object
example: | properties:
/api/v1/images/e5187160a7b2c496773c1c5a45bfd3ffbf25eaa5969328e6469d36f31cf240a3 urls:
http://127.0.0.1:8188/view?filename=ComfyUI_00001_.png&type=output type: array
image/png: items:
schema: type: string
description: The PNG binary content. example:
type: string uris: [ "/api/v1/images/e5187160a7b2c496773c1c5a45bfd3ffbf25eaa5969328e6469d36f31cf240a3", "http://127.0.0.1:8188/view?filename=ComfyUI_00001_.png&type=output" ]
format: binary
204: 204:
description: | description: |
The prompt was run but did not contain any SaveImage outputs, so nothing will be returned. The prompt was run but did not contain any SaveImage outputs, so nothing will be returned.

View File

@ -3,6 +3,7 @@ import asyncio
import glob import glob
import struct import struct
import sys import sys
import shutil
from PIL import Image, ImageOps from PIL import Image, ImageOps
from io import BytesIO from io import BytesIO
@ -560,9 +561,15 @@ class PromptServer():
content_digest = digest(prompt_dict) content_digest = digest(prompt_dict)
cache_path = os.path.join(user_data_dir("comfyui", "comfyanonymous", roaming=False), content_digest) cache_path = os.path.join(user_data_dir("comfyui", "comfyanonymous", roaming=False), content_digest)
cache_url = f"/api/v1/images/{content_digest}"
if os.path.exists(cache_path): if os.path.exists(cache_path):
return web.FileResponse(path=cache_path, return web.Response(status=200,
headers={"Content-Disposition": f"filename=\"{content_digest}.png\""}) headers={
"Digest": f"SHA-256={content_digest}",
"Location": f"/api/v1/images/{content_digest}",
},
body=json.dumps({'urls': [cache_url]}))
# todo: check that the files specified in the InputFile nodes exist # todo: check that the files specified in the InputFile nodes exist
@ -591,29 +598,25 @@ class PromptServer():
'ui']: 'ui']:
images = node['ui']['images'] images = node['ui']['images']
for image_tuple in images: for image_tuple in images:
subfolder_ = image_tuple['subfolder'] filename_ = image_tuple['abs_path']
filename_ = image_tuple['filename'] output_images.append(filename_)
output_images.append(PromptServer.get_output_path(subfolder=subfolder_, filename=filename_))
if len(output_images) > 0: if len(output_images) > 0:
image_ = output_images[-1] image_ = output_images[-1]
if not os.path.exists(os.path.dirname(cache_path)): if not os.path.exists(os.path.dirname(cache_path)):
os.makedirs(os.path.dirname(cache_path)) try:
os.symlink(image_, cache_path) os.makedirs(os.path.dirname(cache_path))
cache_url = "/api/v1/images/{content_digest}" except:
pass
shutil.copy(image_, cache_path)
filename = os.path.basename(image_) filename = os.path.basename(image_)
if 'Accept' in request.headers and request.headers['Accept'] == 'text/uri-list': comfyui_url = f"http://{self.address}:{self.port}/view?filename={filename}&type=output"
res = web.Response(status=200, text=f""" return web.Response(status=200,
{cache_url} headers={
http://{self.address}:{self.port}/view?filename={filename}&type=output "Digest": f"SHA-256={content_digest}",
""") "Location": f"/api/v1/images/{content_digest}",
else: "Content-Disposition": f"filename=\"{filename}\""},
res = web.FileResponse(path=image_, body=json.dumps({'urls': [cache_url, comfyui_url]}))
headers={
"Digest": f"SHA-256={content_digest}",
"Location": f"/api/v1/images/{content_digest}",
"Content-Disposition": f"filename=\"{filename}\""})
return res
else: else:
return web.Response(status=204) return web.Response(status=204)

View File

@ -1263,8 +1263,10 @@ class SaveImage:
metadata.add_text(x, json.dumps(extra_pnginfo[x])) metadata.add_text(x, json.dumps(extra_pnginfo[x]))
file = f"{filename}_{counter:05}_.png" file = f"{filename}_{counter:05}_.png"
img.save(os.path.join(full_output_folder, file), pnginfo=metadata, compress_level=4) abs_path = os.path.join(full_output_folder, file)
img.save(abs_path, pnginfo=metadata, compress_level=4)
results.append({ results.append({
"abs_path": os.path.abspath(abs_path),
"filename": file, "filename": file,
"subfolder": subfolder, "subfolder": subfolder,
"type": self.type "type": self.type