lincolnloop / python-qrcode

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

DataOverFlow error #174

Closed brunobl closed 3 years ago

brunobl commented 5 years ago

Code below results in DataOverFlow error.

bruno$ python3 qr.py
Traceback (most recent call last):
  File "qr.py", line 33, in <module>
    qr.make(fit=True)
  File "/usr/local/lib/python3.7/site-packages/qrcode/main.py", line 93, in make
    self.best_fit(start=self.version)
  File "/usr/local/lib/python3.7/site-packages/qrcode/main.py", line 165, in best_fit
    raise exceptions.DataOverflowError()
qrcode.exceptions.DataOverflowError

If reducing the number of generated qrcodes, it works fine.

# pip install Pillow
# pip install qrcode
# pip install lxml
# https://ourcodeworld.com/articles/read/554/how-to-create-a-qr-code-image-or-svg-in-python

# Import QR Code library
import qrcode
import qrcode.image.svg

# Create qr code instance
qr = qrcode.QRCode(
    version = 1,
    error_correction = qrcode.constants.ERROR_CORRECT_M,
    box_size = 10,
    border = 4,
)

factory = qrcode.image.svg.SvgPathImage

allqr = ["032539","032546","032553","032560","032577","032584","032591","032607","032614","032621","032638","032645","032652","032669","032676","032683","032690","032706","032713","032720","032737","032744","032751","032768","032775","032782","032799","032805","032812","032829","032836","032843","032850","040008","040015","040022","040039","040046","040053","040060","040077","040084","040091","040107","040114","040121","040138","040145","040152","040169","040176","040183","040190","040206","040213","040220","040237","040244","040251","040268","040275","040282","040299","040305","040312","040329","040336","040343","040350","040367","040374","040381","040398","040404","040411","040428","040435","040442","040459","040466","040473","040480","040497","040503","040510","040527","040534","040541","040558","040565","040572","040589","040596","040602","040619","040626","040633","040640","040657","040664","040671","040688","040695","040701","040718","040725","040732","040749","040756","040763","040770","040787","040794","040800","040817","040824","040831","040848","040855","040862","040879","040886","040893","040909","040916","040923","040930","040947","040954","040961","040978","040985","040992","041005","041012","041029","041036","041043","041050","041067","041074","041081","041098","041104","041111","041128","041135","041142","041159"]

for numero in allqr:

    # The data that you want to store
    data = "http://qr.rokfishing.com/"+numero

    # Add data
    qr.add_data(data)
    qr.make(fit=True)

    # Create an image from the QR Code instance
    img = qrcode.make(data, image_factory = factory)

    # Save it somewhere, change the extension as needed:
    img.save("qrcode"+numero+".svg")
heuer commented 5 years ago
> for numero in allqr:
>    # The data that you want to store
>   data = "http://qr.rokfishing.com/"+numero
>
>     # Add data
>    qr.add_data(data)
>    qr.make(fit=True)

You're adding the data to an existing QR Code within the loop. The QR Code contains after the first two loops: "http://qr.rokfishing.com/032539http://qr.rokfishing.com/032546"

You've to reset the QR Code's data in the loop via "clear()":

for numero in allqr:
    # The data that you want to store
    data = "http://qr.rokfishing.com/"+numero

    # This is important, otherwise the data of the previously 
    # generated QR Code is still available!
    qr.clear()

    # Add data
    qr.add_data(data)
    qr.make(fit=True)
# [...]