From 99abc0eb2ef172993e80c49ae88f2416e5354d34 Mon Sep 17 00:00:00 2001 From: pythongosssss <125205205+pythongosssss@users.noreply.github.com> Date: Thu, 9 Mar 2023 17:57:59 +0000 Subject: [PATCH] Changed to upload to input dir Fixed jpg Added dupe support Changed to use existing nodes --- nodes.py | 8 -------- server.py | 16 ++++++++++++---- web/extensions/core/uploadImage.js | 4 +++- web/scripts/widgets.js | 2 +- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/nodes.py b/nodes.py index 49d76fae..33092b33 100644 --- a/nodes.py +++ b/nodes.py @@ -833,9 +833,6 @@ class LoadImage: m.update(f.read()) return m.digest().hex() -class UploadImage(LoadImage): - input_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "uploads") - class LoadImageMask: input_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "input") @classmethod @@ -871,9 +868,6 @@ class LoadImageMask: m.update(f.read()) return m.digest().hex() -class UploadImageMask(LoadImageMask): - input_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "uploads") - class ImageScale: upscale_methods = ["nearest-exact", "bilinear", "area"] crop_methods = ["disabled", "center"] @@ -925,9 +919,7 @@ NODE_CLASS_MAPPINGS = { "LatentUpscale": LatentUpscale, "SaveImage": SaveImage, "LoadImage": LoadImage, - "UploadImage": UploadImage, "LoadImageMask": LoadImageMask, - "UploadImageMask": UploadImageMask, "ImageScale": ImageScale, "ImageInvert": ImageInvert, "ConditioningCombine": ConditioningCombine, diff --git a/server.py b/server.py index 3dce72e5..fbab4e0c 100644 --- a/server.py +++ b/server.py @@ -72,7 +72,7 @@ class PromptServer(): @routes.post("/upload/image") async def upload_image(request): - upload_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "uploads") + upload_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "input") if not os.path.exists(upload_dir): os.makedirs(upload_dir) @@ -85,7 +85,15 @@ class PromptServer(): if not filename: return web.Response(status=400) - with open(os.path.join(upload_dir, filename), "wb") as f: + split = os.path.splitext(filename) + i = 1 + while os.path.exists(os.path.join(upload_dir, filename)): + filename = f"{split[0]} ({i}){split[1]}" + i += 1 + + filepath = os.path.join(upload_dir, filename) + + with open(filepath, "wb") as f: f.write(image.file.read()) return web.json_response({"name" : filename}) @@ -97,12 +105,12 @@ class PromptServer(): async def view_image(request): if "file" in request.match_info: type = request.rel_url.query.get("type", "output") - if type != "output" and type != "uploads": + if type != "output" and type != "input": return web.Response(status=400) output_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), type) file = request.match_info["file"] - file = os.path.splitext(os.path.basename(file))[0] + ".png" + file = os.path.basename(file) file = os.path.join(output_dir, file) if os.path.isfile(file): return web.FileResponse(file) diff --git a/web/extensions/core/uploadImage.js b/web/extensions/core/uploadImage.js index aca40b3e..45fabb78 100644 --- a/web/extensions/core/uploadImage.js +++ b/web/extensions/core/uploadImage.js @@ -1,9 +1,11 @@ import { app } from "/scripts/app.js"; +// Adds an upload button to the nodes + app.registerExtension({ name: "Comfy.UploadImage", async beforeRegisterNodeDef(nodeType, nodeData, app) { - if (nodeData.name === "UploadImage" || nodeData.name === "UploadImageMask") { + if (nodeData.name === "LoadImage" || nodeData.name === "LoadImageMask") { nodeData.input.required.upload = ["IMAGEUPLOAD"]; } }, diff --git a/web/scripts/widgets.js b/web/scripts/widgets.js index 76933f9d..55bdd8f1 100644 --- a/web/scripts/widgets.js +++ b/web/scripts/widgets.js @@ -141,7 +141,7 @@ export const ComfyWidgets = { node.imgs = [img]; app.graph.setDirtyCanvas(true); }; - img.src = `/view/${name}?type=uploads`; + img.src = `/view/${name}?type=input`; } // Add our own callback to the combo widget to render an image when it changes