[Python] Custom QRcodes with python qrcode module

Discussão em 'Python' iniciado por Stack, Fevereiro 15, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to generate a customized qrcode, with a logo in the center, with the Python qrcode module. Searching the web, many threads agree on using the PILImage module to compose the image with the 'composite' method and a function that returns a mask

    def style_eyes(img):
    img_size = img.size[0]
    eye_size = 70 #default
    quiet_zone = 40 #default
    mask = Image.new('L', img.size, 0)
    draw = ImageDraw.Draw(mask)
    draw.rectangle((40, 40, 110, 110), fill=255)
    draw.rectangle((img_size-110, 40, img_size-40, 110), fill=255)
    draw.rectangle((40, img_size-110, 110, img_size-40), fill=255)
    return mask


    but apparently there is an error with the axes referring to the rectangle

    self.draw.draw_rectangle(xy, fill, 1) ValueError: x1 must be greater than or equal to x0

    Other topics take other approaches, but none of them worked.

    From https://pypi.org/project/qrcode/ i got that

    import qrcode
    from qrcode.image.styledpil import StyledPilImage
    from qrcode.image.styles.moduledrawers.pil import RoundedModuleDrawer
    from qrcode.image.styles.colormasks import RadialGradiantColorMask

    qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L)
    qr.add_data('Some data')

    img_1 = qr.make_image(image_factory=StyledPilImage, module_drawer=RoundedModuleDrawer())
    img_2 = qr.make_image(image_factory=StyledPilImage, color_mask=RadialGradiantColorMask())
    img_3 = qr.make_image(image_factory=StyledPilImage, embeded_image_path="/path/to/image.png")


    and I get two overlapping images.

    In fact, the documentation available is very dry, without details on how to use the methods. I would like to know what the best approach would actually be and where to find out, if there is freely available, a complete documentation with all features, including methods and parameters.

    I want a custom qrcode and I get syntax errors or two overlapping images.

    Continue reading...

Compartilhe esta Página