This repository has been archived by the owner on Dec 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupload_images.py
94 lines (82 loc) · 3.83 KB
/
upload_images.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import os
import json
from comfy.cli_args import args
from PIL import Image
from PIL.PngImagePlugin import PngInfo
import numpy as np
import requests
class UploadImages:
def __init__(self):
self.type = "output"
self.compress_level = 4
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"images": ("IMAGE", {"tooltip": "The images to upload."}),
"note": ("STRING", {"tooltip": "A note to associate with the image."}),
"email": ("STRING", {"tooltip": "The email to use for authentication."}),
"url": ("STRING", {"default": "https://toolkit.instantstudio.ai/api/v1/creations", "tooltip": "The URL to post the image to."}),
"api_key": ("STRING", {"default": "TODO", "tooltip": "The API Key to use for authentication."})
},
"hidden": {
"prompt": "PROMPT", "extra_pnginfo": "EXTRA_PNGINFO"
},
}
RETURN_TYPES = ()
FUNCTION = "upload_images"
OUTPUT_NODE = True
CATEGORY = "Instant Studio"
DESCRIPTION = "Uploads the input images to Instant Studio."
def upload_images(self, images, note=None, email=None, url=None, api_key=None, prompt=None, extra_pnginfo=None):
uuids = None
succeeded_count = 0
failed_count = 0
temp_file_paths = list()
try:
for image in images:
i = 255. * image.cpu().numpy()
img = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8))
metadata = None
if not args.disable_metadata:
metadata = PngInfo()
if prompt is not None:
for key in prompt:
if "inputs" in prompt[key] and "api_key" in prompt[key]["inputs"]:
prompt[key]["inputs"]["api_key"] = "TODO"
metadata.add_text("prompt", json.dumps(prompt))
if extra_pnginfo is not None:
if "workflow" in extra_pnginfo and "nodes" in extra_pnginfo["workflow"]:
for node in extra_pnginfo['workflow']['nodes']:
if node['type'] == 'UploadImagesToInstantStudio':
node['widgets_values'] = []
for x in extra_pnginfo:
metadata.add_text(x, json.dumps(extra_pnginfo[x]))
temp_file = None
import tempfile
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
temp_file_path = temp_file.name
img.save(temp_file_path, pnginfo=metadata, compress_level=self.compress_level)
temp_file_paths.append(temp_file_path)
files = [("files[]", open(temp_file_path, "rb")) for temp_file_path in temp_file_paths]
headers = {
"Authorization": f"Bearer {api_key}",
"X-User-Email": email
}
params = {
"note": note,
}
response = requests.post(url, headers=headers, params=params, files=files)
if response.status_code != 200:
failed_count += 1
raise Exception(f"Failed to post image: {response.status_code} - {response.text}")
else:
succeeded_count += 1
response_data = response.json()
uuids = response_data.get("uuids")
finally:
for temp_file_path in temp_file_paths:
if temp_file_path and os.path.exists(temp_file_path):
os.remove(temp_file_path)
print(f"Successfully uploaded {succeeded_count} images to Instant Studio: {uuids}")
return { "ui": { "uuids": uuids }, "result": (uuids,) }