new stuff added

This commit is contained in:
chibiace
2023-12-07 13:21:18 +13:00
committed by GitHub
parent 622e4774b9
commit 935ed3bea2
17 changed files with 1500 additions and 23 deletions
+437 -23
View File
@@ -2,24 +2,28 @@ import torch
import folder_paths
import comfy.sd
import comfy.utils
import comfy.sample
import comfy.samplers
from comfy.cli_args import args
from PIL import Image, ImageOps, ImageFont, ImageDraw
from PIL import Image, ImageOps, ImageFont, ImageDraw, ExifTags
from PIL.PngImagePlugin import PngInfo
import numpy as np
import re
import random
import os
import time
import json
import math
import hashlib
import latent_preview
### GLOBALS ###
MAX_RESOLUTION=32768
base_path = os.path.dirname(os.path.realpath(__file__))
models_dir = os.path.join(base_path, "extras")
extras_dir = os.path.join(base_path, "extras")
folder_paths.folder_names_and_paths["chibi-wildcards"] = ([os.path.join(models_dir, "chibi-wildcards")], {".txt"})
folder_paths.folder_names_and_paths["chibi-wildcards"] = ([os.path.join(extras_dir, "chibi-wildcards")], {".txt"})
folder_paths.folder_names_and_paths["fonts"] = ([os.path.join(extras_dir, "fonts")], {".ttf"})
@@ -266,11 +270,7 @@ class LoadEmbedding:
class ConditionText:
class ConditionTextMulti:
def __init__(self):
pass
@@ -296,6 +296,8 @@ class ConditionText:
def conditiontext(self, clip, first='', second='', third='', fourth='', ):
emptystring = ""
returnedcond = []
#!I probably want to fix this mess at some point.
if first != '':
firstraw = clip.tokenize(first)
first_cond, first_pooled = clip.encode_from_tokens(firstraw, return_pooled=True)
@@ -335,6 +337,33 @@ class ConditionText:
return (clip,returnedcond[0],returnedcond[1],returnedcond[2],returnedcond[3],)
class ConditionText:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"clip": ("CLIP",),
"text" : ("STRING", {"forceInput": True},),
}}
RETURN_TYPES = ("CLIP","CONDITIONING",)
FUNCTION = "conditiontext"
CATEGORY = "Chibi-Nodes/Text"
def conditiontext(self, clip, text=None ):
if text != None:
tokens = clip.tokenize(text)
else:
tokens = clip.tokenize("")
cond, pooled = clip.encode_from_tokens(tokens, return_pooled=True)
return (clip,[[cond, {"pooled_output": pooled}]],)
class SaveImages:
def __init__(self):
pass
@@ -344,19 +373,20 @@ class SaveImages:
return {
"required": {
"filename_type":(["Timestamp","Fixed","Fixed Single"],),
"fixed_filename":("STRING",{"default":"output"})
"fixed_filename":("STRING",{"default":"output",})
},
"optional":{
"images" : ("IMAGE",),
"latents" : ("LATENT",),
"vae" : ("VAE",),
"fixed_filename_override":("STRING",{"forceInput": True},)
},
"hidden": {"prompt": "PROMPT", "extra_pnginfo": "EXTRA_PNGINFO"},
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("images",)
RETURN_TYPES = ("IMAGE","STRING",)
RETURN_NAMES = ("images","filename_list",)
FUNCTION = "saveimage"
OUTPUT_NODE = True
CATEGORY = "Chibi-Nodes"
@@ -367,13 +397,17 @@ class SaveImages:
def IS_CHANGED(s,seed):
seed = random.seed()
def saveimage(self,filename_type,fixed_filename,vae=None, latents=None, images=None, prompt=None, extra_pnginfo=None):
def saveimage(self,filename_type,fixed_filename,fixed_filename_override=None,vae=None, latents=None, images=None, prompt=None, extra_pnginfo=None):
if fixed_filename_override != None:
fixed_filename_override = fixed_filename_override.rsplit(".",1)[0]
fixed_filename = fixed_filename_override
now = str(round(time.time()))
results = list()
filename_list = []
counter = 0
if images != None:
full_output_folder, filename, counter, subfolder, filename_prefix = folder_paths.get_save_image_path(now, folder_paths.get_output_directory(), images[0].shape[1], images[0].shape[0])
@@ -396,6 +430,7 @@ class SaveImages:
if filename_type == "Fixed Single":
file = f"{fixed_filename}.png"
filename_list.append(file)
img.save(os.path.join(full_output_folder, file), pnginfo=metadata, compress_level=4)
results.append({
"filename": file,
@@ -427,6 +462,8 @@ class SaveImages:
file = f"{fixed_filename}_{counter:03}.png"
if filename_type == "Fixed Single":
file = f"{fixed_filename}.png"
filename_list.append(file)
img.save(os.path.join(full_output_folder, file), pnginfo=metadata, compress_level=4)
results.append({
"filename": file,
@@ -437,7 +474,7 @@ class SaveImages:
return_results = decoded_latents
# return { "ui": { "images": results }}
return {"ui": { "images": results },"result": (return_results,)}
return {"ui": { "images": results },"result": (return_results,str(filename_list),)}
@@ -510,14 +547,15 @@ class ImageSimpleResize:
"edge":(["largest","smallest","all","width","height"],),
},
"optional":{
"size_override":("INT",{"forceInput": True})
"size_override":("INT",{"forceInput": True}),
"vae":("VAE",)
}}
RETURN_TYPES = ("IMAGE",)
RETURN_TYPES = ("IMAGE","LATENT")
OUTPUT_NODE = False
FUNCTION = "imagesimpleresize"
CATEGORY = "Chibi-Nodes/Image"
def imagesimpleresize(self, image, size, edge, size_override=None):
def imagesimpleresize(self, image, size, edge, size_override=None, vae=None):
if size_override:
size = size_override
@@ -552,7 +590,7 @@ class ImageSimpleResize:
image = image.resize((round(size/ratio),round(size)), Image.LANCZOS)
if width < height:
if size < width:
image = ImageOps.contain(image, (MAX_RESOLUTION,size), Image.LANCZOS)
image = ImageOps.contain(image, (size,MAX_RESOLUTION), Image.LANCZOS)
else:
image = image.resize((round(size),round(size*ratio)), Image.LANCZOS)
if width == height:
@@ -576,7 +614,20 @@ class ImageSimpleResize:
image = np.array(image).astype(np.float32) / 255.0
image = torch.from_numpy(image)[None,]
return(image,)
if vae != None:
latent = image
x = (latent.shape[1] // 8) * 8
y = (latent.shape[2] // 8) * 8
if latent.shape[1] != x or latent.shape[2] != y:
x_offset = (latent.shape[1] % 8) // 2
y_offset = (latent.shape[2] % 8) // 2
latent = latent[:, x_offset:x + x_offset, y_offset:y + y_offset, :]
latent = vae.encode(latent[:,:,:,:3])
return (image,{"samples":latent})
else:
return(image,None,)
class Int2String:
def __init__(self):
@@ -594,21 +645,384 @@ class Int2String:
CATEGORY = "Chibi-Nodes/Text"
def int2string(self, Int):
print(Int)
return(str(Int),)
class LoadImageExtended:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
input_dir = folder_paths.get_input_directory()
files = [f for f in os.listdir(input_dir) if os.path.isfile(os.path.join(input_dir, f))]
return {"required":
{"image": (sorted(files), {"image_upload": True}),
},
"optional":{"vae": ("VAE", )}
}
CATEGORY = "Chibi-Nodes/Image"
#changes here
RETURN_TYPES = ("IMAGE", "MASK", "LATENT", "STRING", "STRING","INT","INT",)
RETURN_NAMES = ("IMAGE", "MASK", "LATENT","filename","image Info","width","height",)
#
FUNCTION = "load_image"
def load_image(self, image, vae=None):
image_path = folder_paths.get_annotated_filepath(image)
filename = image_path.rsplit('/',1)[-1]
im = Image.open(image_path)
### Start ai-info.py section, with no exif
def type_changer(value):
if value.isnumeric():
return int(value)
else:
return value
im.load()
prompt = {}
if "prompt" in im.info.keys():
#comfyui, workflow is also available but we aren't getting that today
# prompt = {}
prompt.update({"prompt":json.loads(im.info["prompt"])})
else:
# automatic111, gosh this is a mess.
if "parameters" in im.info.keys():
parameters = im.info["parameters"]
prompt = {"parameters": {}}
parameters = re.split('(Negative prompt): |(Negative Template): |(Template): |(ControlNet): |\n',parameters)
#removes None and new lines
parameters_clean_none = []
for i in range(0,len(parameters)):
if parameters[i] == None:
pass
elif parameters[i] == "":
pass
else:
parameters_clean_none.append(parameters[i])
parameters = parameters_clean_none
#settings field
parameters_settings = {}
for i in range(0,len(parameters)):
if parameters[i].split(":",1)[0] == "Steps":
parameters[i] = re.split(", ",parameters[i])
for k in parameters[i]:
k = k.split(": ",1)
if len(k) == 2:
k[1] = type_changer(k[1])
#makes "Size" : "(widthxheight)" into two keys
if k[0] == "Size":
k[1] = k[1].split("x")
for s in range(0,len(k[1])):
k[1][s] = type_changer(k[1][s])
parameters_settings.update({"width" : k[1][0]})
parameters_settings.update({"height" : k[1][1]})
else:
parameters_settings.update({k[0]:k[1]})
parameters[i] = parameters_settings
#builder
parameters_built = {}
for i in range(0,len(parameters)):
match parameters[i]:
case "Negative prompt":
parameters_built.update({parameters[i]:parameters[i+1]})
case "Negative Template":
parameters_built.update({parameters[i]:parameters[i+1]})
case "Template":
parameters_built.update({parameters[i]:parameters[i+1]})
case "ControlNet":
parameters_built.update({parameters[i]:parameters[i+1]})
case dict():
parameters_built.update(parameters[i])
case _:
if i == 0:
parameters_built.update({"Positive prompt": parameters[i]})
pass
prompt["parameters"] = parameters_built
if type(prompt) == dict:
prompt = json.dumps(prompt,indent=2)
elif type(prompt) == str:
prompt = json.dumps(json.loads(prompt),indent=2)
###end section
im = ImageOps.exif_transpose(im)
image = im.convert("RGB")
image = np.array(image).astype(np.float32) / 255.0
image = torch.from_numpy(image)[None,]
shape = image.shape
width = shape[2]
height = shape[1]
if 'A' in im.getbands():
mask = np.array(im.getchannel('A')).astype(np.float32) / 255.0
mask = 1. - torch.from_numpy(mask)
else:
mask = torch.zeros((64,64), dtype=torch.float32, device="cpu")
if vae != None:
latent = image
x = (latent.shape[1] // 8) * 8
y = (latent.shape[2] // 8) * 8
if latent.shape[1] != x or latent.shape[2] != y:
x_offset = (latent.shape[1] % 8) // 2
y_offset = (latent.shape[2] % 8) // 2
latent = latent[:, x_offset:x + x_offset, y_offset:y + y_offset, :]
latent = vae.encode(latent[:,:,:,:3])
return (image, mask.unsqueeze(0),{"samples":latent},filename,str(prompt),width,height,)
else:
return (image, mask.unsqueeze(0),None,filename,str(prompt),width,height,)
@classmethod
def IS_CHANGED(s, image, vae=None):
image_path = folder_paths.get_annotated_filepath(image)
m = hashlib.sha256()
with open(image_path, 'rb') as f:
m.update(f.read())
return m.digest().hex()
@classmethod
def VALIDATE_INPUTS(s, image, vae=None):
if not folder_paths.exists_annotated_filepath(image):
return "Invalid image file: {}".format(image)
return True
class SimpleSampler:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {"required":
{
"model": ("MODEL",),
"sampler":(["Normal - euler","Normal - uni_pc","LCM Lora - lcm","SDXL Turbo - dpmpp_sde karras"],),
"positive": ("CONDITIONING", ),
"negative": ("CONDITIONING", ),
"latents": ("LATENT", ),
"mode": (["txt2img","img2img"],)
},
"optional": {
"seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff,"forceInput": True}),
}
}
RETURN_TYPES = ("LATENT",)
FUNCTION = "sample"
CATEGORY = "Chibi-Nodes"
def IS_CHANGED(s,seed):
seed = random.seed()
def sample(self, model,sampler, positive, negative, latents, mode,seed=None, scheduler="normal",sampler_name="euler"):
# ['euler', 'euler_ancestral', 'heun', 'heunpp2', 'dpm_2', 'dpm_2_ancestral', 'lms', 'dpm_fast', 'dpm_adaptive','dpmpp_2s_ancestral', 'dpmpp_sde', 'dpmpp_sde_gpu', 'dpmpp_2m', 'dpmpp_2m_sde', 'dpmpp_2m_sde_gpu', 'dpmpp_3m_sde', 'dpmpp_3m_sde_gpu', 'ddpm', 'lcm', 'ddim', 'uni_pc', 'uni_pc_bh2']
# ['normal', 'karras', 'exponential', 'sgm_uniform', 'simple', 'ddim_uniform']
match sampler:
case "Normal - euler":
sampler_name = "uni_pc"
steps = 20
cfg = 7
case "Normal - uni_pc":
sampler_name = "uni_pc"
steps = 20
cfg = 7
case "LCM Lora - lcm":
sampler_name = "lcm"
steps = 8
cfg = 1.8
case "SDXL Turbo - dpmpp_sde karras":
sampler_name = "ddmpp_sde"
steps = 8
cfg = 1.8
scheduler = "karras"
case _:
steps = 20
cfg = 7
match mode:
case "txt2img":
denoise = 1.0
case "img2img":
denoise = 0.6
case _:
denoise = 1.0
if seed == None:
seed = random.seed()
seed = math.floor(random.random() * 10000000000000000)
latent_image = latents["samples"]
batch_inds = latents["batch_index"] if "batch_index" in latents else None
noise = comfy.sample.prepare_noise(latent_image, seed, batch_inds)
noise_mask = None
if "noise_mask" in latents:
noise_mask = latents["noise_mask"]
callback = latent_preview.prepare_callback(model, steps)
samples = comfy.sample.sample(model=model, noise=noise, steps=steps, cfg=cfg, sampler_name=sampler_name, scheduler=scheduler, positive=positive, negative=negative, latent_image=latent_image,
denoise=denoise, disable_noise=False, start_step=0, last_step=steps,
force_full_denoise=True, noise_mask=noise_mask, callback=callback, disable_pbar=False, seed=seed)
out = latents.copy()
out["samples"] = samples
return (out,)
class SeedGenerator:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required":{
"mode":(["Random","Fixed"],),
"fixed_seed":("INT",{"default": 8008135, "min": 0, "max": 0xffffffffffffffff, "step": 1})
},}
RETURN_TYPES = ("INT","STRING",)
RETURN_NAMES = ("seed","text")
OUTPUT_NODE = False
FUNCTION = "generator"
CATEGORY = "Chibi-Nodes/Numbers"
def IS_CHANGED(s,fixed_seed):
seed = random.seed()
def generator(self, mode,fixed_seed):
if mode == "Random":
fixed_seed = math.floor(random.random() * 10000000000000000)
if mode == "Fixed":
fixed_seed = fixed_seed
return(fixed_seed,str(fixed_seed),)
class ImageAddText:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return{
"required":{
"text": ("STRING",{"default":"Chibi-Nodes","multiline":True},),
"font" : [sorted(folder_paths.get_filename_list("fonts"))],
"font_size":("INT",{"default": 24, "min": 0, "max": 200, "step": 1}),
"font_colour":(["black","white","red","green","blue"],),
"invert_mask":([False,True],),
"position_x":("INT",{"default": 0, "min": 0, "max": MAX_RESOLUTION, "step": 1}),
"position_y":("INT",{"default": 0, "min": 0, "max": MAX_RESOLUTION, "step": 1}),
"width":("INT",{"default": 512, "min": 0, "max": MAX_RESOLUTION, "step": 1}),
"height":("INT",{"default": 512, "min": 0, "max": MAX_RESOLUTION, "step": 1})
},
"optional":{
"image": ("IMAGE",),
}
}
RETURN_TYPES = ("IMAGE","MASK","STRING",)
RETURN_NAMES = ("IMAGE","MASK","text",)
FUNCTION = "addtext"
CATEGORY = "Chibi-Nodes/Image"
def addtext(self, text, width, height,font,font_size,position_x,position_y,font_colour,invert_mask,image=None):
if image != None:
width = image.shape[2]
height = image.shape[1]
image = Image.fromarray(np.clip(255. * image[0].cpu().numpy(),0,255).astype(np.uint8))
image = image.convert("RGBA")
else:
image = Image.new('RGBA', (width,height), (255, 255, 255, 0))
text_image = Image.new('RGBA', (width,height), (0, 255, 255, 0))
imaget = ImageDraw.Draw(text_image,)
msg = text
imaget.fontmode = 'L'
fnt = ImageFont.truetype(folder_paths.get_full_path("fonts", font), font_size)
imaget.text((position_x,position_y),msg,font=fnt,fill=font_colour)
if 'A' in text_image.getbands():
mask = np.array(text_image.getchannel('A')).astype(np.float32) / 255.0
mask = 1. - torch.from_numpy(mask)
else:
mask = torch.zeros((64,64), dtype=torch.float32, device="cpu")
image.paste(text_image,(0,0),text_image)
image = ImageOps.exif_transpose(image)
image = image.convert("RGB")
image = np.array(image).astype(np.float32) / 255.0
image = torch.from_numpy(image)[None,]
if invert_mask:
mask = 1.0 - mask
return (image,mask.unsqueeze(0),text,)
NODE_CLASS_MAPPINGS = {
"Loader":Loader,
"SimpleSampler" : SimpleSampler,
"Prompts": Prompts,
"ImageTool": ImageTool,
"Wildcards": Wildcards,
"LoadEmbedding": LoadEmbedding,
"ConditionText": ConditionText,
"SaveImages":SaveImages,
"ConditionTextMulti": ConditionTextMulti,
"Textbox":Textbox,
"ImageSizeInfo" : ImageSizeInfo,
"ImageSimpleResize" : ImageSimpleResize,
"Int2String": Int2String,
"ImageAddText" : ImageAddText,
"Int2String": Int2String,
"LoadImageExtended": LoadImageExtended,
"SeedGenerator" : SeedGenerator,
"SaveImages":SaveImages,
}
File diff suppressed because one or more lines are too long
+96
View File
@@ -0,0 +1,96 @@
-------------------------------
UBUNTU FONT LICENCE Version 1.0
-------------------------------
PREAMBLE
This licence allows the licensed fonts to be used, studied, modified and
redistributed freely. The fonts, including any derivative works, can be
bundled, embedded, and redistributed provided the terms of this licence
are met. The fonts and derivatives, however, cannot be released under
any other licence. The requirement for fonts to remain under this
licence does not require any document created using the fonts or their
derivatives to be published under this licence, as long as the primary
purpose of the document is not to be a vehicle for the distribution of
the fonts.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this licence and clearly marked as such. This may
include source files, build scripts and documentation.
"Original Version" refers to the collection of Font Software components
as received under this licence.
"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to
a new environment.
"Copyright Holder(s)" refers to all individuals and companies who have a
copyright ownership of the Font Software.
"Substantially Changed" refers to Modified Versions which can be easily
identified as dissimilar to the Font Software by users of the Font
Software comparing the Original Version with the Modified Version.
To "Propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification and with or without charging
a redistribution fee), making available to the public, and in some
countries other activities as well.
PERMISSION & CONDITIONS
This licence does not grant any rights under trademark law and all such
rights are reserved.
Permission is hereby granted, free of charge, to any person obtaining a
copy of the Font Software, to propagate the Font Software, subject to
the below conditions:
1) Each copy of the Font Software must contain the above copyright
notice and this licence. These can be included either as stand-alone
text files, human-readable headers or in the appropriate machine-
readable metadata fields within text or binary files as long as those
fields can be easily viewed by the user.
2) The font name complies with the following:
(a) The Original Version must retain its name, unmodified.
(b) Modified Versions which are Substantially Changed must be renamed to
avoid use of the name of the Original Version or similar names entirely.
(c) Modified Versions which are not Substantially Changed must be
renamed to both (i) retain the name of the Original Version and (ii) add
additional naming elements to distinguish the Modified Version from the
Original Version. The name of such Modified Versions must be the name of
the Original Version, with "derivative X" where X represents the name of
the new work, appended to that name.
3) The name(s) of the Copyright Holder(s) and any contributor to the
Font Software shall not be used to promote, endorse or advertise any
Modified Version, except (i) as required by this licence, (ii) to
acknowledge the contribution(s) of the Copyright Holder(s) or (iii) with
their explicit written permission.
4) The Font Software, modified or unmodified, in part or in whole, must
be distributed entirely under this licence, and must not be distributed
under any other licence. The requirement for fonts to remain under this
licence does not affect any document created using the Font Software,
except any version of the Font Software extracted from a document
created using the Font Software may only be distributed under this
licence.
TERMINATION
This licence becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER
DEALINGS IN THE FONT SOFTWARE.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

After

Width:  |  Height:  |  Size: 185 KiB