Skip to content

Commit e7ed992

Browse files
committed
add image to image workflow
1 parent fd90246 commit e7ed992

File tree

6 files changed

+67
-3
lines changed

6 files changed

+67
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
output*
22
workflow*
3+
input*
34
__pycache__/
45
.DS_Store*

api/websocket_api.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,30 @@
99
from PIL import Image
1010
import io
1111
from utils.helpers.find_node import find_node
12-
12+
from requests_toolbelt import MultipartEncoder
1313

1414
server_address='127.0.0.1:8188'
1515
client_id=str(uuid.uuid4())
1616

1717
ws = websocket.WebSocket()
1818
ws.connect("ws://{}/ws?clientId={}".format(server_address, client_id))
1919

20+
def setup_image(input_path, name, type="input", overwrite=False):
21+
with open(input_path, 'rb') as file:
22+
multipart_data = MultipartEncoder(
23+
fields={
24+
'image': (name, file, 'image/jpeg'), # Adjust the content-type accordingly
25+
'type': type,
26+
'overwrite': str(overwrite).lower()
27+
}
28+
)
29+
30+
data = multipart_data
31+
headers = {'Content-Type': multipart_data.content_type}
32+
request = urllib.request.Request("http://{}/upload/image".format(server_address), data=data, headers=headers)
33+
with urllib.request.urlopen(request) as response:
34+
return response.read()
35+
2036
def generate_image_by_prompt(prompt, output_path, save_previews=False):
2137
prompt_id = queue_prompt(prompt)['prompt_id']
2238
track_progress(prompt, ws, prompt_id)

main.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from utils.actions.prompt_to_image import prompt_to_image
2+
from utils.actions.prompt_image_to_image import prompt_image_to_image
23
from utils.actions.load_workflow import load_workflow
34
from api.websocket_api import clear_comfy_cache
45
from api.websocket_api import get_image
@@ -8,9 +9,11 @@
89
def main():
910
try:
1011
print("Welcome to the program!")
11-
workflow = load_workflow('./workflows/image_refeed_workflow.json')
12+
workflow = load_workflow('./workflows/image_to_image.json')
1213

13-
prompt_to_image(workflow, 'beautiful woman sitting on a desk in a nice restaurant, candlelight dinner atmosphere, wearing a red dress', save_previews=True)
14+
# prompt_to_image(workflow, 'beautiful woman sitting on a desk in a nice restaurant, candlelight dinner atmosphere, wearing a red dress', save_previews=True)
15+
input_path = './input/ComfyUI_00103_.png'
16+
prompt_image_to_image(workflow, input_path, 'beautiful [white woman], (dark lighting), curly blond hair', save_previews=True)
1417
except Exception as e:
1518
print(f"An error occurred: {e}")
1619
exit_program()

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Pillow==10.0.0
22
Pillow==10.2.0
3+
requests_toolbelt==1.0.0
34
websocket_client==1.7.0
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from api.websocket_api import generate_image_by_prompt
2+
from api.websocket_api import setup_image
3+
from utils.helpers.find_node import find_node
4+
from utils.helpers.randomize_seed import generate_random_15_digit_number
5+
from utils.helpers.replace_key import replace_key
6+
from utils.helpers.find_parent import find_parent_of_key
7+
import json
8+
def prompt_image_to_image(workflow, input_path, positve_prompt, negative_prompt='', save_previews=False):
9+
prompt = json.loads(workflow)
10+
replace_key(prompt, 'seed', generate_random_15_digit_number())
11+
id_to_class_type = {id: details['class_type'] for id, details in prompt.items()}
12+
k_sampler = [key for key, value in id_to_class_type.items() if value == 'KSampler'][0]
13+
14+
postive_input_id = prompt.get(k_sampler)['inputs']['positive'][0]
15+
prompt.get(postive_input_id)['inputs']['text_g'] = positve_prompt
16+
prompt.get(postive_input_id)['inputs']['text_l'] = positve_prompt
17+
18+
if negative_prompt != '':
19+
negative_input_id = prompt.get(k_sampler)['inputs']['negative'][0]
20+
id_to_class_type.get(negative_input_id)['inputs']['text_g'] = negative_prompt
21+
id_to_class_type.get(negative_input_id)['inputs']['text_l'] = negative_prompt
22+
23+
24+
image_loader = [key for key, value in id_to_class_type.items() if value == 'LoadImage'][0]
25+
filename = input_path.split('/')[-1]
26+
prompt.get(image_loader)['inputs']['image'] = filename
27+
28+
setup_image(input_path, filename)
29+
generate_image_by_prompt(prompt, './output/', save_previews)
30+
31+
32+
33+

utils/helpers/find_parent.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def find_parent_of_key(json_obj, key, parent=None):
2+
if key in json_obj:
3+
return parent
4+
for k, v in json_obj.items():
5+
if isinstance(v, dict):
6+
# Pass the current object as the parent for the next level of recursion
7+
parent_obj = find_parent_of_key(v, key, parent=json_obj)
8+
if parent_obj is not None:
9+
return parent_obj
10+
return None

0 commit comments

Comments
 (0)