How To Create a QR Code Generator Using Python Script

How To Create a QR Code Generator Using Python Script

In today’s digital age, QR codes have become ubiquitous, offering a convenient way to share information quickly and easily. With Python’s versatile capabilities, creating a QR code generator is both simple and practical. This guide will walk you through the process of building your own QR code generator using Python, allowing you to generate QR codes for various purposes, such as sharing URLs, contact information, or any other text-based data. By the end of this tutorial, you’ll have a functional QR code generator that you can customize and use for your projects.

Love to learn by watching video and doing it side by side then you can watch the video below

Code:

Create a new Python file(stac.py in my case) and add the code below

import qrcode
import os

qr = qrcode.QRCode(version=1, box_size=10, border=5)
qr.add_data("Subscribe To CodingMadeEasy")
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
img.save("qr_code.png")

# Open the QR code image using the default image viewer
os.system("start qr_code.png")

Output:

Above Code Explanation:

  • import qrcode: This line imports the qrcode module, which allows you to generate QR codes in Python.
  • import os: This line imports the os module, which provides a way to interact with the operating system, in this case, to open the QR code image file.

The next few lines generate a QR code with the message “Subscribe To CodingMadeEasy”:

  • qr = qrcode.QRCode(version=1, box_size=10, border=5): This line creates a new QRCode object with specified version, box size, and border.
  • qr.add_data("Subscribe To CodingMadeEasy"): This line adds the data (message) to be encoded into the QR code.
  • qr.make(fit=True): This line generates the QR code, fitting the size to the data.

The following lines create an image from the QR code and save it as a file named “qr_code.png”:

  • img = qr.make_image(fill_color="black", back_color="white"): This line creates an image representation of the QR code with black as the fill color and white as the background color.
  • img.save("qr_code.png"): This line saves the image as a file named “qr_code.png”.

Finally, the code uses os.system("start qr_code.png") to open the saved QR code image using the default image viewer on the system.

Let’s Summarize

Creating a QR code generator using Python can be a rewarding and practical endeavor. Whether you’re looking to share information, create promotional materials, or simply explore the world of coding, building a QR code generator is a fun project that can expand your programming skills. With the knowledge gained from this tutorial, you can further customize your QR code generator, integrate it into other applications, or explore additional features and functionalities. By mastering this project, you’ll not only enhance your programming skills but also have a useful tool at your disposal for generating QR codes whenever you need them.

Author

Sona Avatar

Written by

Leave a Reply

Trending

CodeMagnet

Your Magnetic Resource, For Coding Brilliance

Programming Languages

Web Development

Data Science and Visualization

Career Section

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4205364944170772"
     crossorigin="anonymous"></script>