How to generate QR code in Python
Hi guys,
In this blog post you're going to learn how you can use python to automate generation of QR codes using qr-code module in less than 5 lines of code
Installation
pip install qrcode
Let's get started
To create a barcode using the QRcode library is straight forward we just use the make ( ) method to do it just as shown in the example below;
>>> import qrcode
>>> img = qrcode.make('Codebook price')
>>> img.save('data.png')
Output:
Once you run the above script it will generate an image with a barcode on your project directory with name data.png.
Sample project
Let's assume you own an electronics store, where you sell various electronics components, and you would like each component in your store to have it's own qrcode to simplify selling.
You can use the knowledge we just learned above to do this, whereby you gonna create a list of all the products in your store using python list.
Then at last you just iterate over the list consisting of component descriptions and then generate the qrcode accordingly just as shown below
import qrcode
products = [
'Arduino',
'Raspberry Pi',
'Resistor',
'Codebook Manual']
for product in products:
qrcode.make(product).save(product+'.png')
Output
When the above code is ran, it going to generate images with qrcode of all elements present in our products list.
Hope you find it interesting, now don't be shy share it with fellow developers on how easy its to create QRcode in python.
You can me on Twitter