forked from ZHO-ZHO-ZHO/ComfyUI-ArtGallery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArtGallery.py
561 lines (442 loc) · 18.1 KB
/
ArtGallery.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
import torch
import json
import os
import re
import platform
from PIL import Image, ImageOps, ImageSequence
import numpy as np
import safetensors.torch
def read_json_file(file_path):
try:
# Open file, load JSON content into python dictionary, and return it.
with open(file_path, 'r', encoding='utf-8') as file:
json_data = json.load(file)
return json_data
except Exception as e:
print(f"An error occurred: {str(e)}")
def get_name(json_data):
# Check that data is a list
if not isinstance(json_data, list):
print("Error: input data must be a list")
return None
names = []
# Iterate over each item in the data list
for item in json_data:
# Check that the item is a dictionary
if isinstance(item, dict):
# Check that 'name' is a key in the dictionary
if 'name' in item:
# Append the value of 'name' to the names list
names.append(item['name'])
return names
def get_prompt(json_data, template_name):
try:
# Check if json_data is a list
if not isinstance(json_data, list):
raise ValueError("Invalid JSON data. Expected a list of templates.")
for template in json_data:
# Check if template contains 'name' and 'tags' fields
if 'name' not in template or 'tags' not in template:
raise ValueError("Invalid template. Missing 'name' or 'tags' field.")
if template['name'] == template_name:
name = template.get('name', "")
tags = template.get('tags', "")
print("Extracted tags:", tags)
return name
# If function hasn't returned yet, no matching template was found
raise ValueError(f"No template found with name '{template_name}'.")
except Exception as e:
print(f"An error occurred: {str(e)}")
def get_img_path(template_name, template_type):
p = os.path.dirname(os.path.realpath(__file__))
# 根据操作系统选择合适的分隔符
if os.name == 'posix': # Unix/Linux/macOS
separator = '/'
elif os.name == 'nt': # Windows
separator = '\\'
else:
separator = '/' # 默认使用斜杠作为分隔符
image_path = os.path.join(p, 'img_lists', template_type) # 使用适当的分隔符构建路径
image_filename = f"{template_name}.png"
full_image_path = image_path + separator + image_filename
return full_image_path
class ArtGallery_Zho:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(self):
# Get current file's directory
p = os.path.dirname(os.path.realpath(__file__))
# Paths for various JSON files
artist_file_path = os.path.join(p, 'lists/artists/artist_list.json')
camera_file_path = os.path.join(p, 'lists/cameras/camera_list.json')
# Read JSON from file
self.artist_data = read_json_file(artist_file_path)
self.camera_data = read_json_file(camera_file_path)
# Retrieve name from JSON data
artist_list = get_name(self.artist_data)
artist_list = ['-'] + artist_list
camera_list = get_name(self.camera_data)
camera_list = ['-'] + camera_list
# Paths for various image files
#artist_image_path = os.path.join(p, 'img_lists/artists/')
max_float_value = 1.75
return {
"required": {
"artist": (artist_list, {
"default": artist_list[0],
}),
"artist_weight": ("FLOAT", {
"default": 1.5,
"step": 0.05,
"min": 0,
"max": max_float_value,
"display": "slider",
}),
"camera": (camera_list, {
"default": camera_list[0],
}),
"camera_weight": ("FLOAT", {
"default": 1.5,
"step": 0.05,
"min": 0,
"max": max_float_value,
"display": "slider",
}),
}
}
RETURN_TYPES = ("STRING","STRING","STRING",)
RETURN_NAMES = ("prompt","artist","camera",)
FUNCTION = "artgallery"
CATEGORY = "Zho模块组/🎨 ArtGallery 艺术画廊"
def artgallery(self, artist="-", artist_weight=1, camera="-", camera_weight=1):
artist = get_prompt(self.artist_data, artist)
camera = get_prompt(self.camera_data, camera)
artist_full_image_path = get_img_path(artist, "artists")
camera_full_image_path = get_img_path(camera, "cameras")
prompt = []
if artist != "-" and artist_weight > 0:
P_artist = f"({artist}:{round(artist_weight, 2)})"
prompt.append(P_artist)
if camera != "-" and camera_weight > 0:
P_camera = f"({camera}:{round(camera_weight, 2)})"
prompt.append(P_camera)
prompt = ", ".join(prompt)
prompt = prompt.lower()
return (prompt, P_artist, P_camera,)
class ArtistsImage_Zho:
@classmethod
def INPUT_TYPES(s):
p = os.path.dirname(os.path.realpath(__file__))
atsimg_dir = os.path.join(p, 'img_lists/artists/')
files = [f for f in os.listdir(atsimg_dir) if os.path.isfile(os.path.join(atsimg_dir, f))]
max_float_value = 1.75
return {
"required": {
"image": (sorted(files), {"image_upload_artist": True}),
"weight": ("FLOAT", {
"default": 1.2,
"step": 0.05,
"min": 0,
"max": max_float_value,
"display": "slider",
}),
}
}
CATEGORY = "Zho模块组/🎨 ArtGallery 艺术画廊"
RETURN_TYPES = ("STRING", "IMAGE",)
RETURN_NAMES = ("name", "image",)
FUNCTION = "artists_image"
def artists_image(self, image, weight=1):
image_full_name = image
image_name = image_full_name.rsplit('.', 1)[0]
image_path = get_img_path(image_name, "artists")
img = Image.open(image_path)
output_images = []
output_masks = []
for i in ImageSequence.Iterator(img):
i = ImageOps.exif_transpose(i)
image = i.convert("RGB")
image = np.array(image).astype(np.float32) / 255.0
image = torch.from_numpy(image)[None,]
if 'A' in i.getbands():
mask = np.array(i.getchannel('A')).astype(np.float32) / 255.0
mask = 1. - torch.from_numpy(mask)
else:
mask = torch.zeros((64,64), dtype=torch.float32, device="cpu")
output_images.append(image)
output_masks.append(mask.unsqueeze(0))
if len(output_images) > 1:
output_image = torch.cat(output_images, dim=0)
output_mask = torch.cat(output_masks, dim=0)
else:
output_image = output_images[0]
output_mask = output_masks[0]
prompt = []
if weight > 0:
P_artist = f"({image_name}:{round(weight, 2)})"
prompt.append(P_artist)
return (P_artist, output_image,)
@classmethod
def IS_CHANGED(s, image):
image_path = get_img_path(image_name, "artists")
m = hashlib.sha256()
with open(image_path, 'rb') as f:
m.update(f.read())
return m.digest().hex()
class CamerasImage_Zho:
@classmethod
def INPUT_TYPES(s):
p = os.path.dirname(os.path.realpath(__file__))
camerasimg_dir = os.path.join(p, 'img_lists/cameras/')
files = [f for f in os.listdir(camerasimg_dir) if os.path.isfile(os.path.join(camerasimg_dir, f))]
max_float_value = 1.75
return {
"required": {
"image": (sorted(files), {"image_upload_camera": True}),
"weight": ("FLOAT", {
"default": 1.2,
"step": 0.05,
"min": 0,
"max": max_float_value,
"display": "slider",
}),
}
}
CATEGORY = "Zho模块组/🎨 ArtGallery 艺术画廊"
RETURN_TYPES = ("STRING", "IMAGE",)
RETURN_NAMES = ("name", "image",)
FUNCTION = "cameras_image"
def cameras_image(self, image, weight=1):
image_full_name = image
image_name = image_full_name.rsplit('.', 1)[0]
image_path = get_img_path(image_name, "cameras")
img = Image.open(image_path)
output_images = []
output_masks = []
for i in ImageSequence.Iterator(img):
i = ImageOps.exif_transpose(i)
image = i.convert("RGB")
image = np.array(image).astype(np.float32) / 255.0
image = torch.from_numpy(image)[None,]
if 'A' in i.getbands():
mask = np.array(i.getchannel('A')).astype(np.float32) / 255.0
mask = 1. - torch.from_numpy(mask)
else:
mask = torch.zeros((64,64), dtype=torch.float32, device="cpu")
output_images.append(image)
output_masks.append(mask.unsqueeze(0))
if len(output_images) > 1:
output_image = torch.cat(output_images, dim=0)
output_mask = torch.cat(output_masks, dim=0)
else:
output_image = output_images[0]
output_mask = output_masks[0]
prompt = []
if weight > 0:
P_camera = f"({image_name}:{round(weight, 2)})"
prompt.append(P_camera)
return (P_camera, output_image,)
@classmethod
def IS_CHANGED(s, image):
image_path = get_img_path(image_name, "cameras")
m = hashlib.sha256()
with open(image_path, 'rb') as f:
m.update(f.read())
return m.digest().hex()
class FilmsImage_Zho:
@classmethod
def INPUT_TYPES(s):
p = os.path.dirname(os.path.realpath(__file__))
filmsimg_dir = os.path.join(p, 'img_lists/films/')
files = [f for f in os.listdir(filmsimg_dir) if os.path.isfile(os.path.join(filmsimg_dir, f))]
max_float_value = 1.75
return {
"required": {
"image": (sorted(files), {"image_upload_film": True}),
"weight": ("FLOAT", {
"default": 1.2,
"step": 0.05,
"min": 0,
"max": max_float_value,
"display": "slider",
}),
}
}
CATEGORY = "Zho模块组/🎨 ArtGallery 艺术画廊"
RETURN_TYPES = ("STRING", "IMAGE",)
RETURN_NAMES = ("name", "image",)
FUNCTION = "films_image"
def films_image(self, image, weight=1):
image_full_name = image
image_name = image_full_name.rsplit('.', 1)[0]
image_path = get_img_path(image_name, "films")
img = Image.open(image_path)
output_images = []
output_masks = []
for i in ImageSequence.Iterator(img):
i = ImageOps.exif_transpose(i)
image = i.convert("RGB")
image = np.array(image).astype(np.float32) / 255.0
image = torch.from_numpy(image)[None,]
if 'A' in i.getbands():
mask = np.array(i.getchannel('A')).astype(np.float32) / 255.0
mask = 1. - torch.from_numpy(mask)
else:
mask = torch.zeros((64,64), dtype=torch.float32, device="cpu")
output_images.append(image)
output_masks.append(mask.unsqueeze(0))
if len(output_images) > 1:
output_image = torch.cat(output_images, dim=0)
output_mask = torch.cat(output_masks, dim=0)
else:
output_image = output_images[0]
output_mask = output_masks[0]
prompt = []
if weight > 0:
P_film = f"({image_name}:{round(weight, 2)})"
prompt.append(P_film)
return (P_film, output_image,)
@classmethod
def IS_CHANGED(s, image):
image_path = get_img_path(image_name, "films")
m = hashlib.sha256()
with open(image_path, 'rb') as f:
m.update(f.read())
return m.digest().hex()
class MovementsImage_Zho:
@classmethod
def INPUT_TYPES(s):
p = os.path.dirname(os.path.realpath(__file__))
movementsimg_dir = os.path.join(p, 'img_lists/movements/')
files = [f for f in os.listdir(movementsimg_dir) if os.path.isfile(os.path.join(movementsimg_dir, f))]
max_float_value = 1.75
return {
"required": {
"image": (sorted(files), {"image_upload_movement": True}),
"weight": ("FLOAT", {
"default": 1.2,
"step": 0.05,
"min": 0,
"max": max_float_value,
"display": "slider",
}),
}
}
CATEGORY = "Zho模块组/🎨 ArtGallery 艺术画廊"
RETURN_TYPES = ("STRING", "IMAGE",)
RETURN_NAMES = ("name", "image",)
FUNCTION = "movements_image"
def movements_image(self, image, weight=1):
image_full_name = image
image_name = image_full_name.rsplit('.', 1)[0]
image_path = get_img_path(image_name, "movements")
img = Image.open(image_path)
output_images = []
output_masks = []
for i in ImageSequence.Iterator(img):
i = ImageOps.exif_transpose(i)
image = i.convert("RGB")
image = np.array(image).astype(np.float32) / 255.0
image = torch.from_numpy(image)[None,]
if 'A' in i.getbands():
mask = np.array(i.getchannel('A')).astype(np.float32) / 255.0
mask = 1. - torch.from_numpy(mask)
else:
mask = torch.zeros((64,64), dtype=torch.float32, device="cpu")
output_images.append(image)
output_masks.append(mask.unsqueeze(0))
if len(output_images) > 1:
output_image = torch.cat(output_images, dim=0)
output_mask = torch.cat(output_masks, dim=0)
else:
output_image = output_images[0]
output_mask = output_masks[0]
prompt = []
if weight > 0:
P_movement = f"({image_name}:{round(weight, 2)})"
prompt.append(P_movement)
return (P_movement, output_image,)
@classmethod
def IS_CHANGED(s, image):
image_path = get_img_path(image_name, "movements")
m = hashlib.sha256()
with open(image_path, 'rb') as f:
m.update(f.read())
return m.digest().hex()
class StylesImage_Zho:
@classmethod
def INPUT_TYPES(s):
p = os.path.dirname(os.path.realpath(__file__))
stylesimg_dir = os.path.join(p, 'img_lists/styles/')
files = [f for f in os.listdir(stylesimg_dir) if os.path.isfile(os.path.join(stylesimg_dir, f))]
max_float_value = 1.75
return {
"required": {
"image": (sorted(files), {"image_upload_style": True}),
"weight": ("FLOAT", {
"default": 1.2,
"step": 0.05,
"min": 0,
"max": max_float_value,
"display": "slider",
}),
}
}
CATEGORY = "Zho模块组/🎨 ArtGallery 艺术画廊"
RETURN_TYPES = ("STRING", "IMAGE",)
RETURN_NAMES = ("name", "image",)
FUNCTION = "styles_image"
def styles_image(self, image, weight=1):
image_full_name = image
image_name = image_full_name.rsplit('.', 1)[0]
image_path = get_img_path(image_name, "styles")
img = Image.open(image_path)
output_images = []
output_masks = []
for i in ImageSequence.Iterator(img):
i = ImageOps.exif_transpose(i)
image = i.convert("RGB")
image = np.array(image).astype(np.float32) / 255.0
image = torch.from_numpy(image)[None,]
if 'A' in i.getbands():
mask = np.array(i.getchannel('A')).astype(np.float32) / 255.0
mask = 1. - torch.from_numpy(mask)
else:
mask = torch.zeros((64,64), dtype=torch.float32, device="cpu")
output_images.append(image)
output_masks.append(mask.unsqueeze(0))
if len(output_images) > 1:
output_image = torch.cat(output_images, dim=0)
output_mask = torch.cat(output_masks, dim=0)
else:
output_image = output_images[0]
output_mask = output_masks[0]
prompt = []
if weight > 0:
P_style = f"({image_name}:{round(weight, 2)})"
prompt.append(P_style)
return (P_style, output_image,)
@classmethod
def IS_CHANGED(s, image):
image_path = get_img_path(image_name, "styles")
m = hashlib.sha256()
with open(image_path, 'rb') as f:
m.update(f.read())
return m.digest().hex()
NODE_CLASS_MAPPINGS = {
"ArtGallery_Zho": ArtGallery_Zho,
"ArtistsImage_Zho": ArtistsImage_Zho,
"CamerasImage_Zho": CamerasImage_Zho,
"FilmsImage_Zho": FilmsImage_Zho,
"MovementsImage_Zho": MovementsImage_Zho,
"StylesImage_Zho": StylesImage_Zho,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"ArtGallery_Zho": "🎨 ArtGallery_Zho",
"ArtistsImage_Zho": "🎨 ArtistsGallery_Zho",
"CamerasImage_Zho": "🎨 CamerasGallery_Zho",
"FilmsImage_Zho": "🎨 FilmsGallery_Zho",
"MovementsImage_Zho": "🎨 MovementsGallery_Zho",
"StylesImage_Zho": "🎨 StylesGallery_Zho",
}