diff --git a/MaskNodes.py b/MaskNodes.py index fc5ee8d..952a6de 100644 --- a/MaskNodes.py +++ b/MaskNodes.py @@ -1111,11 +1111,13 @@ def INPUT_TYPES(cls): return { "required": { "text": ("STRING", {"multiline": True}), + "resize": (["Scale", "Pad", "None"], {"default": "Scale"}), "size": ("INT", {"default": 512, "min": 64, "max": 4096, "step": 64}), "qr_version": ("INT", {"default": 1, "min": 1, "max": 40, "step": 1}), "error_correction": (["L", "M", "Q", "H"], {"default": "H"}), "box_size": ("INT", {"default": 10, "min": 1, "max": 100, "step": 1}), "border": ("INT", {"default": 4, "min": 0, "max": 100, "step": 1}), + "pad_color": (["White", "Grey", "Black"], {"default": "White"}), }, } @@ -1124,7 +1126,7 @@ def INPUT_TYPES(cls): CATEGORY = "Masquerade Nodes" - def create_qr_code(self, text, size, qr_version, error_correction, box_size, border): + def create_qr_code(self, text, resize, size, qr_version, error_correction, box_size, border, pad_color): ensure_package("qrcode") import qrcode if error_correction =="L": @@ -1144,9 +1146,18 @@ def create_qr_code(self, text, size, qr_version, error_correction, box_size, bor qr.add_data(text) qr.make(fit=True) img = qr.make_image(fill_color="black", back_color="white") - img = img.resize((size,size)) + if resize == "Scale": + img = img.resize((size, size)) # Convert img (a PIL Image) into a torch tensor - tensor = torch.from_numpy(np.array(img)) + tensor = torch.from_numpy(np.array(img)).float() + + if resize == "Pad": + # if the size is larger then pad + if tensor.shape[1] < size: + padding = (size - tensor.shape[1]) // 2 + color = {"White": 1.0, "Grey": 0.5, "Black": 0.0}.get(pad_color, 0) + tensor = torchfn.pad(tensor, [padding, padding, padding, padding], value=color) + return (tensor2rgb(tensor.unsqueeze(0)),) def rgb2hsv(rgb): diff --git a/README.md b/README.md index 4d54904..a902a90 100644 --- a/README.md +++ b/README.md @@ -277,11 +277,13 @@ This node is the same as Unary Mask Op, but will operate across all channels of ## Create QR Code #### Inputs * `text` - The content to embed in the QR Code +* `resize` - how to resize 'Scale' will scale the qr code, 'Pad' will pad if the qr code is smaller then the size, 'None' will do nothing. * `size` - The size of the QR Code (across height and width) in pixels. * `qr_version` - The version of QR Code to use. Higher versions can encode more data, but are larger. * `error_correction` - The level of error correction to use. * `box_size` - The size of each box in the QR Code in pixels. * `border` - The size of the border around the QR Code in pixels. +* `pad_color` - The color to pad with, 'White', 'Grey', 'Black' ## Convert Color Space #### Inputs diff --git a/examples/qrcode-monster-controlnet.json b/examples/qrcode-monster-controlnet.json new file mode 100644 index 0000000..8dc3139 --- /dev/null +++ b/examples/qrcode-monster-controlnet.json @@ -0,0 +1,1219 @@ +{ + "last_node_id": 50, + "last_link_id": 120, + "nodes": [ + { + "id": 6, + "type": "Reroute", + "pos": [ + 603.2845951379386, + 27.707613676490737 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 5, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 8 + } + ], + "outputs": [ + { + "name": "", + "type": "MODEL", + "links": [ + 9 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + } + }, + { + "id": 7, + "type": "Reroute", + "pos": [ + 1237, + 30 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 11, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 9 + } + ], + "outputs": [ + { + "name": "", + "type": "MODEL", + "links": [ + 10 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + } + }, + { + "id": 4, + "type": "Reroute", + "pos": [ + 508, + 90 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 6, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 3 + } + ], + "outputs": [ + { + "name": "", + "type": "CLIP", + "links": [ + 4, + 5 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + } + }, + { + "id": 2, + "type": "CLIPTextEncode", + "pos": [ + 633, + 93 + ], + "size": { + "0": 399.8425598144531, + "1": 123.48873138427734 + }, + "flags": {}, + "order": 12, + "mode": 0, + "inputs": [ + { + "name": "clip", + "type": "CLIP", + "link": 4 + } + ], + "outputs": [ + { + "name": "CONDITIONING", + "type": "CONDITIONING", + "links": [ + 14 + ], + "shape": 3, + "slot_index": 0 + } + ], + "title": "CLIP Text Encode (Positive Prompt)", + "properties": { + "Node name for S&R": "CLIPTextEncode" + }, + "widgets_values": [ + "Snowy Mountain cabin with pine trees, 16k, uhd, award winning, highly detailed" + ], + "color": "#223", + "bgcolor": "#335" + }, + { + "id": 11, + "type": "ControlNetLoader", + "pos": [ + 66, + 259 + ], + "size": { + "0": 472.8611755371094, + "1": 76.4640121459961 + }, + "flags": {}, + "order": 0, + "mode": 0, + "outputs": [ + { + "name": "CONTROL_NET", + "type": "CONTROL_NET", + "links": [ + 30 + ], + "shape": 3, + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "ControlNetLoader" + }, + "widgets_values": [ + "control_v1p_sd15_qrcode_monster_v2.safetensors" + ] + }, + { + "id": 17, + "type": "Reroute", + "pos": [ + 558, + 472 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 4, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 30 + } + ], + "outputs": [ + { + "name": "", + "type": "CONTROL_NET", + "links": [ + 25 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + } + }, + { + "id": 18, + "type": "Reroute", + "pos": [ + 951, + 477 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 10, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 25 + } + ], + "outputs": [ + { + "name": "", + "type": "CONTROL_NET", + "links": [ + 26 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + } + }, + { + "id": 22, + "type": "Reroute", + "pos": [ + 1865, + 638 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 14, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 33 + } + ], + "outputs": [ + { + "name": "", + "type": "VAE", + "links": [ + 34 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + } + }, + { + "id": 21, + "type": "Reroute", + "pos": [ + 674, + 637 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 7, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 32 + } + ], + "outputs": [ + { + "name": "", + "type": "VAE", + "links": [ + 33 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + } + }, + { + "id": 25, + "type": "UpscaleModelLoader", + "pos": [ + 1868, + 428 + ], + "size": { + "0": 315, + "1": 58 + }, + "flags": {}, + "order": 1, + "mode": 0, + "outputs": [ + { + "name": "UPSCALE_MODEL", + "type": "UPSCALE_MODEL", + "links": [ + 38 + ], + "shape": 3, + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "UpscaleModelLoader" + }, + "widgets_values": [ + "ESRGAN_4x.pth" + ] + }, + { + "id": 3, + "type": "CLIPTextEncode", + "pos": [ + 633, + 272 + ], + "size": { + "0": 394.1120910644531, + "1": 189.6284942626953 + }, + "flags": {}, + "order": 13, + "mode": 0, + "inputs": [ + { + "name": "clip", + "type": "CLIP", + "link": 5 + } + ], + "outputs": [ + { + "name": "CONDITIONING", + "type": "CONDITIONING", + "links": [ + 43 + ], + "shape": 3, + "slot_index": 0 + } + ], + "title": "CLIP Text Encode (Negative Prompt)", + "properties": { + "Node name for S&R": "CLIPTextEncode" + }, + "widgets_values": [ + "blurry, (poorly drawn face:1.3), (bad anatomy:1.4), (bad proportions:1.5), dehydrated, (ugly:1.2), morbid, (mutated hands:1.4), extra fingers, extra arms, extra legs, (fused fingers:1.2), (long neck:1.3), (low quality:1.5), (low-res:1.4), (worst quality:1.6), (deformed:1.5), (disfigured:1.4), malformed limbs, missing arms, missing legs, (mutated:1.4), mutilated, (gross proportions:1.2), (blurry:1.3), (cropped:1.2), (out of frame:1.2), (duplicate:1.3), (cloned face:1.3), (extra limbs:1.3), (JPEG artifacts:1.2), (error:1.1), (username:1.6), (signature:1.6), (watermark:1.6), (text:1.6)\n" + ], + "color": "#322", + "bgcolor": "#533" + }, + { + "id": 5, + "type": "KSampler", + "pos": [ + 1685, + 62 + ], + "size": { + "0": 315, + "1": 262 + }, + "flags": {}, + "order": 19, + "mode": 0, + "inputs": [ + { + "name": "model", + "type": "MODEL", + "link": 10 + }, + { + "name": "positive", + "type": "CONDITIONING", + "link": 17 + }, + { + "name": "negative", + "type": "CONDITIONING", + "link": 44 + }, + { + "name": "latent_image", + "type": "LATENT", + "link": 36 + } + ], + "outputs": [ + { + "name": "LATENT", + "type": "LATENT", + "links": [ + 20 + ], + "shape": 3, + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "KSampler" + }, + "widgets_values": [ + 86279305602227, + "randomize", + 50, + 4, + "dpmpp_2m_sde", + "normal", + 1 + ] + }, + { + "id": 1, + "type": "CheckpointLoaderSimple", + "pos": [ + 72, + 68 + ], + "size": { + "0": 410.642822265625, + "1": 117.83045196533203 + }, + "flags": {}, + "order": 2, + "mode": 0, + "outputs": [ + { + "name": "MODEL", + "type": "MODEL", + "links": [ + 8 + ], + "shape": 3, + "slot_index": 0 + }, + { + "name": "CLIP", + "type": "CLIP", + "links": [ + 3 + ], + "shape": 3, + "slot_index": 1 + }, + { + "name": "VAE", + "type": "VAE", + "links": [ + 32 + ], + "shape": 3, + "slot_index": 2 + } + ], + "properties": { + "Node name for S&R": "CheckpointLoaderSimple" + }, + "widgets_values": [ + "Fantasy/revAnimated_v122.safetensors" + ] + }, + { + "id": 24, + "type": "ImageUpscaleWithModel", + "pos": [ + 2289, + 429 + ], + "size": { + "0": 241.79998779296875, + "1": 46 + }, + "flags": {}, + "order": 21, + "mode": 0, + "inputs": [ + { + "name": "upscale_model", + "type": "UPSCALE_MODEL", + "link": 38 + }, + { + "name": "image", + "type": "IMAGE", + "link": 111 + } + ], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "links": [ + 112 + ], + "shape": 3, + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "ImageUpscaleWithModel" + } + }, + { + "id": 13, + "type": "VAEDecode", + "pos": [ + 2089.375393449767, + 88.39890276696067 + ], + "size": { + "0": 210, + "1": 46 + }, + "flags": {}, + "order": 20, + "mode": 0, + "inputs": [ + { + "name": "samples", + "type": "LATENT", + "link": 20 + }, + { + "name": "vae", + "type": "VAE", + "link": 34 + } + ], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "links": [ + 111, + 113 + ], + "shape": 3, + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "VAEDecode" + } + }, + { + "id": 48, + "type": "SaveImage", + "pos": [ + 2024, + 709 + ], + "size": { + "0": 594.291259765625, + "1": 605.5518798828125 + }, + "flags": {}, + "order": 23, + "mode": 0, + "inputs": [ + { + "name": "images", + "type": "IMAGE", + "link": 112 + } + ], + "properties": {}, + "widgets_values": [ + "qrcode_%date:MM_dd_yyyy%/qr_hires_" + ] + }, + { + "id": 49, + "type": "SaveImage", + "pos": [ + 2453.0990710449205, + 58.733161468505756 + ], + "size": { + "0": 342.85223388671875, + "1": 310.2010803222656 + }, + "flags": {}, + "order": 22, + "mode": 0, + "inputs": [ + { + "name": "images", + "type": "IMAGE", + "link": 113 + } + ], + "properties": {}, + "widgets_values": [ + "qrcode_%date:MM_dd_yyyy%/qr_lores_" + ] + }, + { + "id": 35, + "type": "PreviewImage", + "pos": [ + 960, + 774 + ], + "size": { + "0": 210, + "1": 246 + }, + "flags": {}, + "order": 15, + "mode": 0, + "inputs": [ + { + "name": "images", + "type": "IMAGE", + "link": 115 + } + ], + "properties": { + "Node name for S&R": "PreviewImage" + } + }, + { + "id": 8, + "type": "EmptyLatentImage", + "pos": [ + 1155, + 528 + ], + "size": { + "0": 315, + "1": 106 + }, + "flags": {}, + "order": 17, + "mode": 0, + "inputs": [ + { + "name": "width", + "type": "INT", + "link": 117, + "widget": { + "name": "width", + "config": [ + "INT", + { + "default": 512, + "min": 64, + "max": 8192, + "step": 8 + } + ] + } + }, + { + "name": "height", + "type": "INT", + "link": 116, + "widget": { + "name": "height", + "config": [ + "INT", + { + "default": 512, + "min": 64, + "max": 8192, + "step": 8 + } + ] + }, + "slot_index": 1 + } + ], + "outputs": [ + { + "name": "LATENT", + "type": "LATENT", + "links": [ + 36 + ], + "shape": 3, + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "EmptyLatentImage" + }, + "widgets_values": [ + 768, + 768, + 1 + ] + }, + { + "id": 50, + "type": "Reroute", + "pos": [ + 934.6552751580807, + 571.8623879092405 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 9, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 118 + } + ], + "outputs": [ + { + "name": "", + "type": "INT", + "links": [ + 116, + 117 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + } + }, + { + "id": 47, + "type": "SeargeIntegerConstant", + "pos": [ + 25, + 585 + ], + "size": { + "0": 315, + "1": 58 + }, + "flags": {}, + "order": 3, + "mode": 0, + "outputs": [ + { + "name": "value", + "type": "INT", + "links": [ + 114, + 118 + ], + "shape": 3, + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "SeargeIntegerConstant" + }, + "widgets_values": [ + 768 + ] + }, + { + "id": 10, + "type": "ControlNetApplyAdvanced", + "pos": [ + 1219, + 281 + ], + "size": { + "0": 315, + "1": 166 + }, + "flags": {}, + "order": 18, + "mode": 0, + "inputs": [ + { + "name": "positive", + "type": "CONDITIONING", + "link": 14, + "slot_index": 0 + }, + { + "name": "negative", + "type": "CONDITIONING", + "link": 43, + "slot_index": 1 + }, + { + "name": "control_net", + "type": "CONTROL_NET", + "link": 26, + "slot_index": 2 + }, + { + "name": "image", + "type": "IMAGE", + "link": 29 + } + ], + "outputs": [ + { + "name": "positive", + "type": "CONDITIONING", + "links": [ + 17 + ], + "shape": 3, + "slot_index": 0 + }, + { + "name": "negative", + "type": "CONDITIONING", + "links": [ + 44 + ], + "shape": 3, + "slot_index": 1 + } + ], + "properties": { + "Node name for S&R": "ControlNetApplyAdvanced" + }, + "widgets_values": [ + 1.5, + 0.30000000000000004, + 0.8999090576171881 + ] + }, + { + "id": 20, + "type": "Reroute", + "pos": [ + 963, + 517 + ], + "size": [ + 75, + 26 + ], + "flags": {}, + "order": 16, + "mode": 0, + "inputs": [ + { + "name": "", + "type": "*", + "link": 120 + } + ], + "outputs": [ + { + "name": "", + "type": "IMAGE", + "links": [ + 29 + ], + "slot_index": 0 + } + ], + "properties": { + "showOutputText": false, + "horizontal": false + } + }, + { + "id": 39, + "type": "Create QR Code", + "pos": [ + 317, + 784 + ], + "size": [ + 400, + 244 + ], + "flags": {}, + "order": 8, + "mode": 0, + "inputs": [ + { + "name": "size", + "type": "INT", + "link": 114, + "widget": { + "name": "size", + "config": [ + "INT", + { + "default": 512, + "min": 64, + "max": 4096, + "step": 64 + } + ] + } + } + ], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "links": [ + 115, + 120 + ], + "shape": 3, + "slot_index": 0 + } + ], + "properties": { + "Node name for S&R": "Create QR Code" + }, + "widgets_values": [ + "Snowy Mountain Cabin", + "Pad", + 512, + 2, + "H", + 16, + 1, + "Grey" + ] + } + ], + "links": [ + [ + 3, + 1, + 1, + 4, + 0, + "*" + ], + [ + 4, + 4, + 0, + 2, + 0, + "CLIP" + ], + [ + 5, + 4, + 0, + 3, + 0, + "CLIP" + ], + [ + 8, + 1, + 0, + 6, + 0, + "*" + ], + [ + 9, + 6, + 0, + 7, + 0, + "*" + ], + [ + 10, + 7, + 0, + 5, + 0, + "MODEL" + ], + [ + 14, + 2, + 0, + 10, + 0, + "CONDITIONING" + ], + [ + 17, + 10, + 0, + 5, + 1, + "CONDITIONING" + ], + [ + 20, + 5, + 0, + 13, + 0, + "LATENT" + ], + [ + 25, + 17, + 0, + 18, + 0, + "*" + ], + [ + 26, + 18, + 0, + 10, + 2, + "CONTROL_NET" + ], + [ + 29, + 20, + 0, + 10, + 3, + "IMAGE" + ], + [ + 30, + 11, + 0, + 17, + 0, + "*" + ], + [ + 32, + 1, + 2, + 21, + 0, + "*" + ], + [ + 33, + 21, + 0, + 22, + 0, + "*" + ], + [ + 34, + 22, + 0, + 13, + 1, + "VAE" + ], + [ + 36, + 8, + 0, + 5, + 3, + "LATENT" + ], + [ + 38, + 25, + 0, + 24, + 0, + "UPSCALE_MODEL" + ], + [ + 43, + 3, + 0, + 10, + 1, + "CONDITIONING" + ], + [ + 44, + 10, + 1, + 5, + 2, + "CONDITIONING" + ], + [ + 111, + 13, + 0, + 24, + 1, + "IMAGE" + ], + [ + 112, + 24, + 0, + 48, + 0, + "IMAGE" + ], + [ + 113, + 13, + 0, + 49, + 0, + "IMAGE" + ], + [ + 114, + 47, + 0, + 39, + 0, + "INT" + ], + [ + 115, + 39, + 0, + 35, + 0, + "IMAGE" + ], + [ + 116, + 50, + 0, + 8, + 1, + "INT" + ], + [ + 117, + 50, + 0, + 8, + 0, + "INT" + ], + [ + 118, + 47, + 0, + 50, + 0, + "*" + ], + [ + 120, + 39, + 0, + 20, + 0, + "*" + ] + ], + "groups": [], + "config": {}, + "extra": {}, + "version": 0.4 +} \ No newline at end of file