lincolnloop / python-qrcode

Python QR Code image generator
https://pypi.python.org/pypi/qrcode
Other
4.34k stars 664 forks source link

QR code `print_ascii` is in fact not ASCII #256

Closed maxnoe closed 2 years ago

maxnoe commented 2 years ago

In [1]: import qrcode

In [2]: from io import StringIO

In [3]: buffer = StringIO()

In [4]: code = qrcode.QRCode()

In [5]: code.add_data("Hello")

In [6]: code.print_ascii(out=buffer)

In [7]: print(buffer.getvalue())
                             
                             
    █▀▀▀▀▀█  ▀▀▀█ █▀▀▀▀▀█    
    █ ███ █ █ █ ▀ █ ███ █    
    █ ▀▀▀ █ ██ ▄▀ █ ▀▀▀ █    
    ▀▀▀▀▀▀▀ █ █ ▀ ▀▀▀▀▀▀▀    
    ▀ ▀█▀▀▀ ▄▀ █▄▄█▀▀██ ▄    
      █▀▀█▀▄▄▀█▄█▄█▀ ██▀     
     ▀▀▀  ▀▀█▀▀ █  █ ▄  ▀    
    █▀▀▀▀▀█ ▄▀▄▀ ▀ ▄█▄██     
    █ ███ █ █▄ █▄█▄▄▀▄ ▀     
    █ ▀▀▀ █ ▀█ ▄█▄█▀▄▄█      
    ▀▀▀▀▀▀▀ ▀▀  ▀   ▀  ▀     
                             
                             

In [8]: buffer.getvalue().encode("ascii")
---------------------------------------------------------------------------
UnicodeEncodeError                        Traceback (most recent call last)
Input In [8], in <module>
----> 1 buffer.getvalue().encode("ascii")

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-28: ordinal not in range(128)

It uses unicode characters in utf-8 encoding.

SmileyChris commented 2 years ago

Meh, it's using the following character codes from the Extended ASCII code page: 255, 223, 220, 219

Maybe a bit misleading method name, but 🤷🏻