Home > Enterprise >  QRCode generated in python
QRCode generated in python

Time:03-24

I wrote some code to generate qrcode in python but at run time it shows this error: Import "qrcode" could not be resolved Pylance(reportMissingImports)

I am using the qrcode library but I am having some problems.

CodePudding user response:

Installation is pretty straight forward via pip install. Run the following command to install python-qrcode and pillow.

pip install qrcode[pil]

Once you are done, continue installing OpenCV-Python with the following command:

pip install opencv-python

If you intend to detect multiple QR codes in a single image, make sure that the opencv-python version is at least 4.3.0. Older versions do not come with the multi detection functionalities.

Add the following import declaration at the top of your Python file.

import qrcode
from PIL import Image

For basic usage, you can simply run the make() function to generate a QR Code based on your input text:

img = qrcode.make('Your input text')

You can utilize the QRCode class, which comes with a lot more controls and properties.

    qr = qrcode.QRCode(
        version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
        box_size=10,
        border=4,
    )

The next step is to call the add_data() function. Pass in the input text of your choice. Continue by appending the following code to generate a QR code with a white background and black fill.

qr.add_data('https://medium.com/@ngwaifoong92')
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white").convert('RGB')

You can save it in as an image file as follow:

img.save("sample.png")

Finally you can read this paper.

  • Related