QRCodeGenerator(size, message)



  • Hi DrawBot fellas!!

    I'm struggling with the QRCodeGenerator() ImageObject() property, I got two errors, the first one is TypeError: Expecting byte-buffer, got str when I try to pass a URL, and when I try to encode the URL TypeError: cannot unpack non-iterable float object

    Is it possible that you can help me to figure out what's happening?

    Here's the way I'd love to use it:

    path = 'https://www.mypath.com'
    
    newPage(1000, 1000)
    qr = ImageObject()
    path = path.encode('utf-8')
    qr.QRCodeGenerator(100, path)
    
    image(qr, (0, 0, width(), height()))
    

    And here's the way I made it with the qrcode module by making a png first and then using it inside my drawbot image

    import qrcode
    path = 'https//:www.mypath.com'
    qr = qrcode.QRCode(
        version = 1,
        error_correction = qrcode.constants.ERROR_CORRECT_L,
        box_size = 40,
        border = 0,
    )
    qr.add_data(path)
    qr.make(fit=True)
    img = qr.make_image(fill_color='black', back_color='white')
    qrpath = 'myQr.png'
    img.save(qrpath)
    image(qrpath, (0, 0))
    

    Thank you!!!



  • hello @eduairet,

    when working with QRCodeGenerator:

    • data must be in bytes
    • size must be a tuple

    this works:

    path = bytes('http://www.drawbot.com/', 'utf-8')
    
    w, h = 31, 31
    
    img = ImageObject()
    img.QRCodeGenerator((w, h), path, 'Q')
    
    size(w, h)
    image(img, (0, 0))
    

    the third parameter (L, M, Q or H) determines the correction level, see the Core Image docs for more info.

    hope this helps! cheers

    ps. the resulting image is tiny (1 pixel per bit). you can scale it by reading the pixel values and redrawing it with shapes:

    path = bytes('http://www.drawbot.com/', 'utf-8')
    
    w, h = 31, 31
    s = 10
    
    img = ImageObject()
    img.QRCodeGenerator((w, h), path, 'Q')
    
    size(w*s, h*s)
    for x in range(w):
        for y in range(h):
            c = imagePixelColor(img, (x, y))
            fill(*c)
            rect(x*s, y*s, s, s)
    


  • @gferreira WOW!!!! you saved my ass again, you're amazing, thanks a lot!!!


  • admin

    maybe the callback should convert incoming str to bytes, or the docs must change. Im make an issue --> https://github.com/typemytype/drawbot/issues/411


Log in to reply