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



  • Using almost the exact same script of @gferreira (only added the generated image on top of the shapes) I get a QR code that is not very crisp.

    Am I doing something wrong? Or has something changed since 2020?

    blurry-qr-code.png

    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)
    
    image(img, (0, 0))
    
    

  • admin

    no idea... but the image is indeed blurry. A quick search for CoreImage filter CIQRCodeGenerator + blurry results in lots of options but none of them apply to DrawBot.

    I would look at some python packages like https://github.com/mnooner256/pyqrcode to build the QR data and draw it with DrawBot, without round tripping to a png or image

    good luck!


  • admin

    in the pip installer: pyqrcode --no-deps

    import pyqrcode
    
    blockSize = 10
    
    qr = pyqrcode.create('http://www.drawbot.com/')
    
    for x, row in enumerate(qr.code):
        for y, bit in enumerate(row):
            if bit:
                rect(x * blockSize, y * blockSize, blockSize, blockSize)
            
    print(qr.code)
    


  • Thanks @frederik



  • I ran into the same issue a few weeks ago and @frederik actually posted another solution in the other QR code thread:
    going through the pixels and checking the lightness/brightness:
    https://forum.drawbot.com/topic/397/qr-code-with-type?_=1681732270808

    Using a module has the nice advantage of checking the size of the QR code after generating it.


  • admin

    hehe, totally forgot about that one... but I prefer the solution with pyqrcode as it provides a list 0/1


Log in to reply