lincolnloop / python-qrcode

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

Alphanumeric fails to create valid inputs for default add_data optimize value #144

Closed nanotek163 closed 6 years ago

nanotek163 commented 7 years ago

The below code which uses the default optimize value in the add_data method fails with the error.

qrcode.exceptions.DataOverflowError: Code length overflow. Data size (132) > size available (128)

import qrcode
qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_M,
)

myStr = '0000000000A0000A00'
print(len(myStr))
qr.add_data(myStr, optimize=20)

qr.make(fit=False)
img = qr.make_image()

It is trying to create a QR for version 1 and medium error correction. The length of the string is 18 long, but a string of 20 long should be capable.

However, if the optimize is changed to 0 as in the below code, it passes. I've repeated this for different error correction levels, different QR versions, python versions, and operating systems. I'm able to repeat the issue. It seems that the issue is from within the optimal_data_chunks method.

import qrcode
qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_M,
)

myStr = '0000000000A0000A00'
print(len(myStr))
qr.add_data(myStr, optimize=0)

qr.make(fit=False)
img = qr.make_image()