How to build a QR Code Generator in Python

Hey amazing people, today we are gonna build a QR Code generator in Python.

How QR Code Generator works?

Our QR code generator takes some data as input. That data can be anything like a URL or some text. And then it creates a QR code from t…


This content originally appeared on DEV Community and was authored by Rishabh Singh ⚡

Hey amazing people, today we are gonna build a QR Code generator in Python.

How QR Code Generator works?

Our QR code generator takes some data as input. That data can be anything like a URL or some text. And then it creates a QR code from that input data. And in the final step, the QR Code is saved as an SVG file. Noe since you have a rough idea of how our QR generator is going to work, let's move on to the Project Setup section and make this magic happen.

Alt Text

Alt Text

Project Setup

Before we jump to coding, we need to install one module for our project. We are talking about pyqrcode module which doesn't come pre-installed with python and hence we have to install it manually.

We can install this by simply running pip install pyqrcode command from the terminal. So let's do that.

pip install pyqrcode
Enter fullscreen mode Exit fullscreen mode

Here we go it's done! Now let's move on to the fun part, the coding part.

Let's Code

Alright so remember the first thing we always do is import required modules into our project. In our case, we need to make use of pyqrcode module which we just installed onto our system. So now let's import it.

import pyqrcode
Enter fullscreen mode Exit fullscreen mode

Awesome! So next thing we need to do is to get some user input to create a QR Code.

data = input("Enter the text or link to generate QR code: ")
Enter fullscreen mode Exit fullscreen mode

Here we are simply using an input() function to obtain user input and storing it in the data variable.

Now it's time we create the QR Code!

qr = pyqrcode.create(data)
Enter fullscreen mode Exit fullscreen mode

Here we are simply using pyqrcode.create() function which is a part of our pyqrcode module. This function will take our user input as a parameter and will generate a QR code. That QR code will then be stored in the qr variable.

However, we cannot see that QR code yet. To see it, we have to export it into a file. That can be easily done using another function... Here's how:

qr.svg('qr_code.svg', scale = 8)
Enter fullscreen mode Exit fullscreen mode

Here we are using svg('FILE_NAME', scale = 8) function were we have to provide the name of the QR code file to be generated and a scale parameter which by default will be 8.

And here we did it. Now go ahead and try this one out on your own. ?

Source Code

You can find the complete source code of this project here -

mindninjaX/Python-Projects-for-Beginners

Support

Thank you so much for reading! I hope you found this beginner project useful.

If you like my work please consider Buying me a Coffee so that I can bring more projects, more articles for you.

https://dev-to-uploads.s3.amazonaws.com/i/5irx7eny4412etlwnc64.png

Also if you have any questions or doubts feel free to contact me on Twitter, LinkedIn & GitHub. Or you can also post a comment/discussion & I will try my best to help you :D


This content originally appeared on DEV Community and was authored by Rishabh Singh ⚡


Print Share Comment Cite Upload Translate Updates
APA

Rishabh Singh ⚡ | Sciencx (2021-02-12T17:26:26+00:00) How to build a QR Code Generator in Python. Retrieved from https://www.scien.cx/2021/02/12/how-to-build-a-qr-code-generator-in-python/

MLA
" » How to build a QR Code Generator in Python." Rishabh Singh ⚡ | Sciencx - Friday February 12, 2021, https://www.scien.cx/2021/02/12/how-to-build-a-qr-code-generator-in-python/
HARVARD
Rishabh Singh ⚡ | Sciencx Friday February 12, 2021 » How to build a QR Code Generator in Python., viewed ,<https://www.scien.cx/2021/02/12/how-to-build-a-qr-code-generator-in-python/>
VANCOUVER
Rishabh Singh ⚡ | Sciencx - » How to build a QR Code Generator in Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/12/how-to-build-a-qr-code-generator-in-python/
CHICAGO
" » How to build a QR Code Generator in Python." Rishabh Singh ⚡ | Sciencx - Accessed . https://www.scien.cx/2021/02/12/how-to-build-a-qr-code-generator-in-python/
IEEE
" » How to build a QR Code Generator in Python." Rishabh Singh ⚡ | Sciencx [Online]. Available: https://www.scien.cx/2021/02/12/how-to-build-a-qr-code-generator-in-python/. [Accessed: ]
rf:citation
» How to build a QR Code Generator in Python | Rishabh Singh ⚡ | Sciencx | https://www.scien.cx/2021/02/12/how-to-build-a-qr-code-generator-in-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.