Spaces:
Starting
on
L40S
Starting
on
L40S
File size: 2,521 Bytes
4450790 |
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 |
import qrcode
import torch
from PIL import Image
from ..log import log
from ..utils import pil2tensor
class MTB_QrCode:
"""Basic QR Code generator."""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"url": ("STRING", {"default": "https://www.github.com"}),
"width": (
"INT",
{"default": 256, "max": 8096, "min": 0, "step": 1},
),
"height": (
"INT",
{"default": 256, "max": 8096, "min": 0, "step": 1},
),
"error_correct": (("L", "M", "Q", "H"), {"default": "L"}),
"box_size": (
"INT",
{"default": 10, "max": 8096, "min": 0, "step": 1},
),
"border": (
"INT",
{"default": 4, "max": 8096, "min": 0, "step": 1},
),
"invert": (("BOOLEAN",), {"default": False}),
}
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "do_qr"
CATEGORY = "mtb/generate"
def do_qr(
self,
*,
url: str,
width: int,
height: int,
error_correct: str,
box_size: int,
border: int,
invert: bool,
) -> tuple[torch.Tensor]:
log.warning(
"This node will soon be deprecated, there are much better alternatives like https://github.com/coreyryanhanson/comfy-qr"
)
if error_correct == "L" or error_correct not in ["M", "Q", "H"]:
error_correct = qrcode.constants.ERROR_CORRECT_L
elif error_correct == "M":
error_correct = qrcode.constants.ERROR_CORRECT_M
elif error_correct == "Q":
error_correct = qrcode.constants.ERROR_CORRECT_Q
else:
error_correct = qrcode.constants.ERROR_CORRECT_H
qr = qrcode.QRCode(
version=1,
error_correction=error_correct,
box_size=box_size,
border=border,
)
qr.add_data(url)
qr.make(fit=True)
back_color = (255, 255, 255) if invert else (0, 0, 0)
fill_color = (0, 0, 0) if invert else (255, 255, 255)
code = qr.make_image(back_color=back_color, fill_color=fill_color)
# that we now resize without filtering
code = code.resize((width, height), Image.NEAREST)
return (pil2tensor(code),)
__nodes__ = [MTB_QrCode]
|