Hello Pythonista, In this article I will share with you how to programmatically generate QRcode in Python, we go do this in less than 5 lines of code.
To follow through with this tutorial you need to have a QRcode python library installed on your computer.
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')
Once you run the above script it will generate an image with a barcode on your project directory with name data.png.
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')
When the above code is ran, it going to generate images with barcode 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.
I recommend to also check these based on your interest;
- How to build a realtime barcode reader in Python
- Realtime vehicle detection in Python
- Make your own plagiarism detector in Python
- An intro to emotion detection from text using Python
- Learn how to train spam filter in Python
- A guide to Twitter sentiment analysis using Python
In case of any suggestion or comment, drop it in the comment box and I will get back to you ASAP.