Agents-MCP-Hackathon/AI-QR-code-generator
0
1import hashlib2import torch3 4from comfy.cli_args import args5 6from PIL import ImageFile, UnidentifiedImageError7 8def conditioning_set_values(conditioning, values={}, append=False):9 c = []10 for t in conditioning:11 n = [t[0], t[1].copy()]12 for k in values:13 val = values[k]14 if append:15 old_val = n[1].get(k, None)16 if old_val is not None:17 val = old_val + val18 19 n[1][k] = val20 c.append(n)21 22 return c23 24def pillow(fn, arg):25 prev_value = None26 try:27 x = fn(arg)28 except (OSError, UnidentifiedImageError, ValueError): #PIL issues #4472 and #2445, also fixes ComfyUI issue #341629 prev_value = ImageFile.LOAD_TRUNCATED_IMAGES30 ImageFile.LOAD_TRUNCATED_IMAGES = True31 x = fn(arg)32 finally:33 if prev_value is not None:34 ImageFile.LOAD_TRUNCATED_IMAGES = prev_value35 return x36 37def hasher():38 hashfuncs = {39 "md5": hashlib.md5,40 "sha1": hashlib.sha1,41 "sha256": hashlib.sha256,42 "sha512": hashlib.sha51243 }44 return hashfuncs[args.default_hashing_function]45 46def string_to_torch_dtype(string):47 if string == "fp32":48 return torch.float3249 if string == "fp16":50 return torch.float1651 if string == "bf16":52 return torch.bfloat1653 54def image_alpha_fix(destination, source):55 if destination.shape[-1] < source.shape[-1]:56 source = source[...,:destination.shape[-1]]57 elif destination.shape[-1] > source.shape[-1]:58 destination = torch.nn.functional.pad(destination, (0, 1))59 destination[..., -1] = 1.060 return destination, source61 